- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个在导航栏中有一个导航项的应用程序。
我试图了解单击按钮时如何使用 facebook sdk 连接到 facebook(身份验证)。
这不是一些特殊的 View Controller 或其他东西。
我见过这个:
http://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/authenticate/
但是在那里我需要在委托(delegate)中创建一些我不能使用的东西(比如 UINavigationController),因为我使用的是 UITabBarController..
如何仅通过推送 UINavigationItem 来实现 facebook 登录和 session 创建?
这是我的 AppDelegate.h:
#import <UIKit/UIKit.h>
#import <FacebookSDK/FacebookSDK.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UITabBarController *tbc;
@property (strong, nonatomic) FBSession *session;
@end
#import "AppDelegate.h"
#import "StatusView.h"
#import "JokesView.h"
#import "HomeView.h"
#import "TopTenView.h"
#import "UploadView.h"
@implementation AppDelegate
@synthesize tbc;
@synthesize window = _window;
@synthesize session = _session;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
UINavigationController*nav1 = [[UINavigationController alloc]init];
UINavigationController*nav2 = [[UINavigationController alloc]init];
UINavigationController*nav3 = [[UINavigationController alloc]init];
UINavigationController*nav4 = [[UINavigationController alloc]init];
UINavigationController*nav5 = [[UINavigationController alloc]init];
StatusView*page1 = [[StatusView alloc]initWithNibName:@"StatusView" bundle:nil];
JokesView*page2 = [[JokesView alloc]initWithNibName:@"JokesView" bundle:nil];
HomeView*page3 = [[HomeView alloc]initWithNibName:@"HomeView" bundle:nil];
TopTenView*page4 = [[TopTenView alloc]initWithNibName:@"TopTenView" bundle:nil];
UploadView*page5 = [[UploadView alloc]initWithNibName:@"UploadView" bundle:nil];
page1.title = @"סטטוסים";
page2.title = @"תמונות";
page3.title = @"ראשי";
page4.title = @"Top 10";
page5.title = @"העלאה";
UITabBarItem *tab1 = [[UITabBarItem alloc] initWithTitle:@"Status"
image:[UIImage imageNamed:@"tbc-status.png"] tag:1];
[nav1 setTabBarItem:tab1];
UITabBarItem *tab2 = [[UITabBarItem alloc] initWithTitle:@"Jokes"
image:[UIImage imageNamed:@"tbc-jokes.png"] tag:1];
[nav2 setTabBarItem:tab2];
UITabBarItem *tab3 = [[UITabBarItem alloc] initWithTitle:@"Home"
image:[UIImage imageNamed:@"tbc-home.png"] tag:1];
[nav3 setTabBarItem:tab3];
UITabBarItem *tab4 = [[UITabBarItem alloc] initWithTitle:@"Tpp10"
image:[UIImage imageNamed:@"tbc-topten.png"] tag:1];
[nav4 setTabBarItem:tab4];
UITabBarItem *tab5 = [[UITabBarItem alloc] initWithTitle:@"Upload"
image:[UIImage imageNamed:@"tbc-upload.png"] tag:1];
[nav5 setTabBarItem:tab5];
[nav1 pushViewController:page1 animated:NO];
[nav2 pushViewController:page2 animated:NO];
[nav3 pushViewController:page3 animated:NO];
[nav4 pushViewController:page4 animated:NO];
[nav5 pushViewController:page5 animated:NO];
tbc = [[UITabBarController alloc]init];
tbc.viewControllers = [NSArray arrayWithObjects:nav5,nav4,nav3,nav2,nav1, nil];
tbc.selectedIndex = 2;
// NavBar Design
UIImage *navbarPortrait = [[UIImage imageNamed:@"topbar.jpg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *navbarLandscape = [[UIImage imageNamed:@"topbar.jpg"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UINavigationBar appearance] setBackgroundImage:navbarPortrait
forBarMetrics:UIBarMetricsDefault];
[[UINavigationBar appearance] setBackgroundImage:navbarLandscape
forBarMetrics:UIBarMetricsLandscapePhone];
// NavBar Design End
// TabBar Design
UIImage *tabBackground = [[UIImage imageNamed:@"tbcb3ack.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UITabBar appearance] setBackgroundImage:tabBackground];
[[tbc tabBar] setBackgroundImage:tabBackground];
// TabBar Design End
[self.window addSubview:tbc.view];
self.window.rootViewController = self.tbc;
// Push Notifications
[[UIApplication sharedApplication] registerForRemoteNotificationTypes:(UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeNone)];
// Push Notifications End
// Facebook Code Start
//UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Not Logged in"
// message:@"You Log in to use all the fearues in this app"
// delegate:nil
// cancelButtonTitle:@"OK"
// otherButtonTitles:nil];
if (FBSession.activeSession.state == FBSessionStateCreatedTokenLoaded) {
// To-do, show logged in view
} else {
//[alert show];
}
// Facebook Code End
[self.window makeKeyAndVisible];
return YES;
}
// Facebook sdk code Start
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
// attempt to extract a token from the url
return [FBAppCall handleOpenURL:url
sourceApplication:sourceApplication
withSession:self.session];
}
// Facebook sdk code End
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
[FBAppCall handleDidBecomeActiveWithSession:self.session];
}
#pragma mark Template generated code
@end
最佳答案
解决这个问题实际上有两个部分。您首先需要将 UIButton 定义为 UINavigationItem,它将在单击时进行登录(或注销)。其次,您的应用必须准备好接受登录事件。我不确定哪个部分给您带来了更多麻烦,但这是您设置第一部分的方法,即按钮:
UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
loginButton.frame = CGRectMake(0.0f, 0.0f, 75.0f, 44.0f);
[loginButton setTitle:@"Login" forState:UIControlStateNormal];
[loginButton addEventHandler:^(id sender)
{
[FBSession openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler:
^(FBSession *session,
FBSessionState state, NSError *error) {
[self sessionStateChanged:session state:state error:error];
}];
}
forControlEvents:UIControlEventTouchUpInside];
[self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:loginButton];
- (void)sessionStateChanged:(FBSession *)session
state:(FBSessionState) state
error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
// Connected to facebook... so go to your next view controller
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Login failed
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
@interface MYNavigationController () <UINavigationControllerDelegate>
@property (nonatomic, strong) UIButton *facebookButton;
@end
@implementation MYNavigationController
- (void)viewDidLoad
{
[super viewDidLoad];
// Set this subclass as its own delegate to be able receive the willShowViewController: method
self.delegate = self;
// Create a shared facebook button
_facebookButton = [UIButton buttonWithType:UIButtonTypeCustom];
_facebookButton.frame = CGRectMake(0.0f, 0.0f, 75.0f, 44.0f);
[_facebookButton setTitle:@"Login" forState:UIControlStateNormal];
[_facebookButton
addTarget:self
action:@selector(onFacebookButtonClick:)
forControlEvents:UIControlEventTouchUpInside];
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// As each view controller is about to be shown, change the view controller's
// navigationItem to have this facebook button as its right bar button
viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.facebookButton];
}
- (void)onFacebookButtonClick:(id)sender
{
// Handle the click event when the facebook button is clicked
// You need to have logic here to know if the session is open or not
// Of course, when the session isn't open, then open a new session (ie. login)
// and when the session IS open, then close the session (ie. logout)
[FBSession
openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler: ^(FBSession *session, FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
// Connected to facebook so...
// 1. change the button text to say "logout" instead of "login"
// 2. go to your next view controller
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Login failed so revert everything to pre-login state
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
UITabBarController
,那么我们可以将其用作
facebookButton
的所有者,在所有
UINavigationControllers
之间共享.在高层次上,逻辑与我之前的代码示例没有任何变化。您正在某处创建一个按钮,该按钮被添加到导航 Controller 显示的每个 View Controller 中。为此,您需要首先创建一个
UITabBarController
子类(同样,我的称为
MYTabBarController
,但您可以随意调用它)。它在
MYTabBarController.m
中看起来像这样:
#import "MYTabBarController.h"
#import <FacebookSDK/FacebookSDK.h>
@implementation MYTabBarController
- (UIButton *)facebookButton
{
if (! _facebookButton)
{
// Create a shared facebook button on demand
_facebookButton = [UIButton buttonWithType:UIButtonTypeCustom];
_facebookButton.frame = CGRectMake(0.0f, 0.0f, 75.0f, 44.0f);
[_facebookButton setTitle:@"Login" forState:UIControlStateNormal];
[_facebookButton
addTarget:self
action:@selector(onFacebookButtonClick:)
forControlEvents:UIControlEventTouchUpInside];
}
return _facebookButton;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
// As each view controller is about to be shown, change the view controller's
// navigationItem to have this facebook button as its right bar button
viewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.facebookButton];
}
- (void)onFacebookButtonClick:(id)sender
{
// Handle the click event when the facebook button is clicked
// You need to have logic here to know if the session is open or not
// Of course, when the session isn't open, then open a new session (ie. login)
// and when the session IS open, then close the session (ie. logout)
[FBSession
openActiveSessionWithReadPermissions:nil
allowLoginUI:YES
completionHandler: ^(FBSession *session, FBSessionState state, NSError *error)
{
[self sessionStateChanged:session state:state error:error];
}];
}
- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState)state error:(NSError *)error
{
switch (state) {
case FBSessionStateOpen:
// Connected to facebook so...
// 1. change the button text to say "logout" instead of "login"
// eg. [self.facebookButton setTitle:@"Logout" forState:UIControlStateNormal];
// 2. go to your next view controller
break;
case FBSessionStateClosed:
case FBSessionStateClosedLoginFailed:
// Login failed so revert everything to pre-login state
[FBSession.activeSession closeAndClearTokenInformation];
break;
default:
break;
}
if (error) {
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Error"
message:error.localizedDescription
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
}
}
MYTabBarController.h
至少需要定义一个按钮并且它符合
UINavigationControllerDelegate
协议(protocol):
@interface MYTabBarController: UITabBarController<UINavigationControllerDelegate>
@property (nonatomic, strong) UIButton *facebookButton;
application:didFinishLaunchingWithOptions:
需要做一个小的改变。它需要将标签栏声明为每个 UINavigationController 的委托(delegate):
// Set the navigation controller delegates to be the tabbar in order to hook into the willShowViewController: method
nav1.delegate = tbc;
nav2.delegate = tbc;
nav3.delegate = tbc;
nav4.delegate = tbc;
nav5.delegate = tbc;
关于iphone - 从 UINavigationBar 登录 Facebook ios sdk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17215905/
我正在细读 http://www.khronos.org/网站,只找到了 OpenCL 的头文件(不是我不关心的 OpenGL)。如何获取 OpenCL SDK? 最佳答案 AMD 的 ATI Str
Android 项目中最低(最低 sdk)和最高(目标 sdk)级别是否有任何影响。这些东西是否会影响项目的可靠性和效率。 最佳答案 没有影响,如果您以 SDK 级别 8 为目标,那么您的应用将以 9
“min sdk version/target sdk version”和“compile sdk version”有什么区别?我知道 min 和 target sdk 是什么意思,但是 compil
我正在尝试运行 Dji Mobile-Sdk-Android:https://github.com/dji-sdk/Mobile-SDK-Android使用 dji 网站上的说明:https://de
我目前正在向我的 iPhone 应用程序添加新的 Facebook iOS sdk 3.1.1。我看到 sdk 有重大变化。例如。我的旧 sdk 创建一个 Facebook 对象并从 FBReques
我最近刚刚下载了 Xcode 4.6 with mac 10.8.4 with iOS 6.0 SDK package,我立即注意到我无法使用我的 iPhone 3Gs with iOS 4.6 进行
我尝试下载 OpenCL SDK。但是没办法。我有一个 AMD GPU,所以我在谷歌上搜索了 AMD SDK,但是来自谷歌的所有链接和一些教程都被破坏了,不可能通过 AMD 开发者网站找到 sdk。
安装 Google Cloud SDK 后,当我运行时 gcloud 授权登录 我收到一条错误消息: Your browser has been opened to visit: https://a
我一直在 nvidia 网站上搜索 GPU 计算 SDK,因为我正在尝试构建具有 cuda 支持的点云库 (PCL)。但是,在 nvidia 网站上,我只能找到工具包的链接,而不是 SDK 的单个下载
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post一个问题。 2
当我打开 Android SDK 管理器时,会出现一个屏幕“选择要安装的包”。它列出了一堆带有绿色复选标记(已经安装)的软件包,还有一些带有 x's 的软件包,它们没有安装。如果我选择“全部接受”,它
在开发过程中,我发布了 SDK 21 Lollipop,但我无法在我的 KitKat 设备上使用它。应用程序非常简单,我只将 SDK 用于动画和 Material 设计,但是当我尝试对 Play 商店
enter image description here friend 们好 在使用 Linux、jenkins 和 docker 探索 dotnet 核心时,我遇到了构建问题,该问题在标题“/usr
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我创建了一个flutter项目,运行flutter packages get,输出如下:【二】flutter包搞定等待另一个 flutter 命令释放启动锁...第二次运行“flutter packa
这个问题在这里已经有了答案: What are the Android SDK build-tools, platform-tools and tools? And which version sh
我使用 Corona SDK 已经快一年了,并且开发了几个简单的游戏。我现在正在寻找的是在 Corona SDK 中创建 3D 幻觉的某种方法。如果有人有 Corona 3D 方面的经验,我将不胜感激
我有一个CLDC 1.1 / MIDP 2.0项目,可以使用Java ME SDK 3.2很好地进行编译。 最近,我已将Java SE升级到8,结果Java ME SDK开始在Windows启动时显示
有没有办法以编程方式控制连接到华擎主板的 RGB 照明?我知道华硕有一个 Aura SDK,但华擎是否也有一个用于他们的硬件? 如果是,我在哪里可以找到它? 最佳答案 没有用于华擎多彩的 sdk。但是
如何使用 crm 2011 sdk 和 XrmServiceContext 创建事务? 在下一个示例中,'new_brand' 是一些自定义实体。我想创建三个品牌。第三个拥有错误的 OwnerID g
我是一名优秀的程序员,十分优秀!