0

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

问题:当用户尝试通过 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;
    }
}
4

1 回答 1

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

try this code in openActiveSessionWithReadPermissions block

于 2013-10-18T05:40:58.063 回答