Facebook iOS SDK没有调用完成处理程序

问题:

使用FB SDK和方法openActiveSessionWithReadPermissions,完成处理程序似乎不会被调用,当应用程序从Facebook的网页或Facebook应用程序重新打开,我得到这个错误输出:

FBSDKLog:从FBSessionStateCreated到FBSessionStateClosed的FBSession INVALID转换FBSDKLog:从FBSessionStateCreated到FBSessionStateCreatedOpening的FBSession转换

上下文

  • 使用Facebook SDK 3.2.1
  • 应用程序是为iOS 5.0+构build的
  • 使用xCode 4.6.1
  • 在模拟器iOS 5.1上testing
  • 在iPhone 4S上testing,运行iOS 6.1.2(不要使用6.0社交框架来testing5.0+的实现)
  • 更新最初为iOS 3.0构build的应用程序,并使用旧版本的共享包,但现在已更新为ARC和共享工具包的实现,我相信已经被注释掉了 – 希望这个问题不是与旧的共享工具包冲突函数,不能在代码中find任何内容

采取措施解决

在整个堆栈溢出中search,发现类似的问题,但没有解决scheme

  • ios6的Facebook集成login总是FBSessionStateClosedLoginFailed永远不会打开 (我已经有bool:应用程序实现)
  • Facebook iOS SDK 3.0 – 会话未打开
  • Facebook SDK 3.1 – validation访问令牌时出错
  • 在FB设置面板中有正确的应用程序包
  • 有正确的Facebook应用程序ID是plist
  • 打开FB日志logging打开FBSettings setLoggingBehavior

具体细节:

这些是我在应用程序中实现Facebook连接的步骤。

  • 我采取的第一步是通过Facebook教程: https : //developers.facebook.com/docs/tutorials/ios-sdk-tutorial/ 。 我得到了第一部分,validation部分按预期工作(我按照教程的指示构build了一个单独的应用程序)。
  • 然后我在我正在更新的应用程序中的教程中采取了相同的步骤,这是行不通的
  • 然后,我按照https://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/ (与教程非常相似)
  • 我又遇到了问题
  • 然后花了很多时间寻找解决scheme,找不到一个

代码中的高级步骤:

  • 我有我的FB方法在AppDelegate中设置
  • 在一个特定的视图控制器我有一个button,调用openSessionWithAllowLoginUI方法来启动login过程

AppDelegate.m

- (void)applicationDidFinishLaunching:(UIApplication *)application { // set up facebook logging [FBSettings setLoggingBehavior:[NSSet setWithObjects:FBLoggingBehaviorFBRequests, FBLoggingBehaviorFBURLConnections, FBLoggingBehaviorAccessTokens, FBLoggingBehaviorSessionStateTransitions, nil]]; // Call the ACAccountStore method renewCredentialsForAccount, which will update the OS's understanding of the token state ACAccountStore *accountStore; ACAccountType *accountTypeFB; if ((accountStore = [[ACAccountStore alloc] init]) && (accountTypeFB = [accountStore accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierFacebook] ) ){ NSArray *fbAccounts = [accountStore accountsWithAccountType:accountTypeFB]; id account; if (fbAccounts && [fbAccounts count] > 0 && (account = [fbAccounts objectAtIndex:0])){ [accountStore renewCredentialsForAccount:account completion:^(ACAccountCredentialRenewResult renewResult, NSError *error) { //we don't actually need to inspect renewResult or error. if (error){ } }]; } } - (void)applicationDidBecomeActive:(UIApplication *)application { // We need to properly handle activation of the application with regards to Facebook Login // (eg, returning from iOS 6.0 Login Dialog or from fast app switching). NSLog(@"Calling app did become active"); [FBSession.activeSession handleDidBecomeActive]; } /* * Callback for session changes. */ - (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error { NSLog(@"Session State Changed"); switch (state) { case FBSessionStateOpen: if (!error) { // We have a valid session NSLog(@"User session found"); } break; case FBSessionStateClosed: case FBSessionStateClosedLoginFailed: [FBSession.activeSession closeAndClearTokenInformation]; break; default: break; } [[NSNotificationCenter defaultCenter] postNotificationName:FBSessionStateChangedNotification object:session]; if (error) { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error" message:error.localizedDescription delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil]; [alertView show]; } } /* * Opens a Facebook session and optionally shows the login UX. */ - (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI { NSLog(@"Openning session with Facebook"); return [FBSession openActiveSessionWithReadPermissions:nil allowLoginUI:allowLoginUI completionHandler:^(FBSession *session, FBSessionState state, NSError *error) { [self sessionStateChanged:session state:state error:error]; }]; } /* * If we have a valid session at the time of openURL call, we handle * Facebook transitions by passing the url argument to handleOpenURL */ - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { // attempt to extract a token from the url NSLog(@"Calling open URL"); return [FBSession.activeSession handleOpenURL:url]; } - (void) closeSession { NSLog(@"Clossing Facebook Sessions"); [FBSession.activeSession closeAndClearTokenInformation]; } - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { NSLog(@"handleOpenUrl Called"); return [FBSession.activeSession handleOpenURL:url]; } 

用button查看控制器

 - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view from its nib. // Register for Facebook change notification [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sessionStateChanged:) name:FBSessionStateChangedNotification object:nil]; } - (IBAction)login:(id)sender { ATIAppDelegate *myAppDelegate = (ATIAppDelegate *)[[UIApplication sharedApplication]delegate]; [myAppDelegate openSessionWithAllowLoginUI:YES]; } 

我认为这个问题可能是什么?

  • 看起来这可能是与令牌的东西?
  • 或者,也许是通知中心?
  • 请注意,在testing过程中,我一直在使用Facebook应用程序访问我的帐户,然后尝试重新login到应用程序,我发现这些问题与其他用户

好吧,我想出来了 – 这个问题与FB本身无关,应用程序(我正在更新其他人的代码)在.plist中有一个设置 – '应用程序不在后台运行'设置为true。

这意味着一旦应用程序从Facebook应用程序或Facebook移动网站重新启动,它不准备处理下一步。

在这里输入图像说明

如果你不想让应用程序在后台运行,你可以绑定到你的AppDelegate中的FBSDKApplicationDelegate ,如下所示:

 import UIKit import FBSDKLoginKit @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate { var window: UIWindow? func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application, didFinishLaunchingWithOptions: launchOptions) } func applicationWillResignActive(_ application: UIApplication) { FBSDKAppEvents.activateApp() } func application(_ application: UIApplication, open url: URL, sourceApplication: String?, annotation: Any) -> Bool { return FBSDKApplicationDelegate.sharedInstance().application(application, open: url, sourceApplication: sourceApplication, annotation: annotation) } } 

https://forums.developer.apple.com/thread/50332 http://ashishkakkad.com/tag/fbsdkapplicationdelegate/