Facebook iOS SDK登录失败,第一次尝试但第二次成功

时间:2013-10-17 06:54:36

标签: iphone ios facebook ios6 facebook-javascript-sdk

好的,所以这对我来说很难调试,因为我没有遇到我的应用程序的许多用户似乎遇到的问题。如果您想自己尝试,可以通过搜索“syncquik”在iTunes上下载该应用程序。

问题:当用户尝试通过Facebook登录按钮登录我的应用时,首次尝试打开活动的FB会话通常会失败。起初,我认为这是因为他们必须在iPhone的常规设置中打开应用程序的Facebook隐私设置。但是,因为有很多人遇到同样的问题,我开始怀疑是不是因为我的代码出错了(见下文)?我按照演示示例和Facebook说明,但也许我错过了什么?提前感谢您提供的任何帮助!

代码:

(在ViewController中按住FB按钮):

- (IBAction)fbSignIn:(id)sender
{
    AppDelegate* appDelegate = [UIApplication sharedApplication].delegate;
    [appDelegate openSession];
}

(在AppDelegate.m中):

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error
{
NSLog(@"FB Session State Changed");
switch (state) {
    case FBSessionStateOpen: {
            NSLog(@"FBSessionStateOpen");
            if (fbButtonPushed && FBSession.activeSession.isOpen) {
                [self sendUserFbInfo];
                fbButtonPushed = NO;
            }
    }
        break;
    case FBSessionStateClosed:
        NSLog(@"FBSessionStateClosed");
        break;
    case FBSessionStateClosedLoginFailed:
        NSLog(@"FBSessionStateClosedLoginFailed");
        [FBSession.activeSession closeAndClearTokenInformation];
        [self showLoginView];
        break;
    default:
        break;
}

if (error) {
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Please Try Again" message:@"If second attempt is unsuccessful, then set SyncQuik to ON in Settings > Facebook." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [alertView show];
    [[NSNotificationCenter defaultCenter] postNotificationName:@"com.syncquik.connectionrequest.complete" object:nil];
}    

}

- (void)openSession
{
    NSLog(@"Opening a User's FB Session");
    fbButtonPushed = YES;
    NSArray *permissons = [[NSArray alloc] initWithObjects:@"email", nil];
    [FBSession openActiveSessionWithReadPermissions:permissons
                                       allowLoginUI:YES
                                  completionHandler:
                                        ^(FBSession *session,
                                          FBSessionState state, NSError *error) {
                [self sessionStateChanged:session state:state error:error];
     }];
}

- (BOOL)application:(UIApplication *)application
    openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    return [FBSession.activeSession handleOpenURL:url];
}

- (void)showLoginView
{
    NSLog(@"Show Login View");
    //Anything else need to be placed here?
}

- (void)applicationDidBecomeActive:(UIApplication *)application
{
    [FBSession.activeSession handleDidBecomeActive];
    if (fbButtonPushed && FBSession.activeSession.isOpen) {
        [self sendUserFbInfo];
        fbButtonPushed = NO;
    }
}

1 个答案:

答案 0 :(得分:0)

if (self.session.state != FBSessionStateCreated) {
        // Create a new, logged out session.
        [self openSession];
    }
 else{
    [FBSession setActiveSession:self.session];
    [self sessionStateChanged:session state:state error:error];
 }

在openActiveSessionWithReadPermissions块中尝试此代码