Facebook iOS SDK未调用完成处理程序 [英] Facebook iOS SDK Not Calling Completion Handler

查看:89
本文介绍了Facebook iOS SDK未调用完成处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:

使用FB SDK和方法openActiveSessionWithReadPermissions,当从Facebook Web或Facebook应用程序重新打开该应用程序时,似乎未调用完成处理程序,并且得到以下错误输出:

Using the FB SDK and the method openActiveSessionWithReadPermissions, the completion handler doesn't appear to get called when the app is reopened from Facebook web or Facebook app and I get this error output:

FBSDKLog:FBSession 无效从FBSessionStateCreated转换为FBSessionStateClosed FBSDKLog:FBSession从FBSessionStateCreated过渡到FBSessionStateCreatedOpening

FBSDKLog: FBSession INVALID transition from FBSessionStateCreated to FBSessionStateClosed FBSDKLog: FBSession transition from FBSessionStateCreated to FBSessionStateCreatedOpening

上下文

  • 使用Facebook SDK 3.2.1
  • 该应用程序是为iOS 5.0+构建的
  • 使用xCode 4.6.1
  • 在模拟器iOS 5.1上进行测试
  • 在运行iOS 6.1.2的iPhone 4S上进行测试(未使用6.0社交框架,因为要测试5.0+版本的实现)
  • 更新最初是为iOS 3.0构建的应用程序,并使用旧版本的sharekit,但现在已针对ARC和共享工具包实现进行了更新,我相信所有评论都已被注释掉-希望该问题与以下版本不冲突:旧的共享工具包功能,无法在代码中找到任何内容

已采取的解决步骤

在整个堆栈溢出中进行搜索,发现提到了类似的问题,但没有解决方案

Searched throughout Stack Overflow, found similar issues mentioned but not a solution

  • ios6 facebook integration login always FBSessionStateClosedLoginFailed never opens (i already have bool:application implemented)
  • Facebook iOS SDK 3.0 - session not open
  • Facebook SDK 3.1 - Error validating access token
  • Have the correct app bundle in the FB settings panel
  • Have the correct Facebook app ID is the plist
  • Have FB logging turned on FBSettings setLoggingBehavior

详细信息:

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

These are the steps I took to implement Facebook connectivity within the app.

  • The first step I took was walking through the Facebook tutorial at: https://developers.facebook.com/docs/tutorials/ios-sdk-tutorial/. I got the first part, the authentication part working as expected (I built a separate app as instructed by the tutorial.)
  • I then took the same steps within the tutorial in the app I'm updating, this did not work
  • I then followed the instructions on https://developers.facebook.com/docs/howtos/login-with-facebook-using-ios-sdk/ (which are very similar to the tutorial)
  • Again I ran into issues
  • Then spent a lot of time searching for solution, could not find one

代码中的高级步骤:

  • 我在AppDelegate中设置了FB方法
  • 在特定的视图控制器中,我有一个按钮,该按钮调用openSessionWithAllowLoginUI方法以启动登录过程

代码

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
    // (e.g., 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];

}

带有按钮的View Controller

- (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];
}

我认为问题可能是什么?

  • 令牌似乎有些东西了?
  • 还是通知中心?
  • 请注意,在测试过程中,我一直在撤消对Facebook应用程序的访问,然后尝试再次登录该应用程序,我发现这些问题导致了其他用户的问题
  • It seems like it may be something with the tokens?
  • Or maybe the notification center?
  • Note that during testing I've been going and revoking access of the Facebook app to my account and then trying to login again to the app, I see these has caused issues with other users

推荐答案

好吧,我知道了-问题与FB本身无关,该应用程序(我正在更新其他人的代码)在其中进行了设置.plist-应用程序不在后台运行"设置为true.

Okay I figured it out - the issue was nothing to do with FB itself, the app (I'm working on updating someone else's code) had a setting in the .plist - 'Application does not run in background' set to true.

意味着一旦该应用程序从Facebook应用程序或Facebook移动网站重新启动,它就不准备进行下一步.

Meaning that once the app was relaunched from the Facebook app or Facebook mobile site it wasn't prepared to handle the next step.

这篇关于Facebook iOS SDK未调用完成处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆