简体   繁体   English

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

[英]Facebook iOS SDK Login Fails On First Attempt but Successful on Second

Ok, so this has been a tough one for me to debug because I do not experience this problem that many users of my app appear to be experiencing. 好的,这对我来说很难调试,因为我没有遇到很多应用程序用户似乎遇到的问题。 If you would like to try it yourself, you may download the app on iTunes by searching for "syncquik". 如果您想自己尝试,可以通过搜索“ syncquik”在iTunes上下载该应用程序。

PROBLEM: When users attempt to login to my app via the Facebook login button, the first attempt to open an active FB session often fails. 问题:当用户尝试通过Facebook登录按钮登录我的应用程序时,第一次打开活动FB会话的尝试通常会失败。 At first, I thought this was because they had to turn on the app's Facebook privacy settings in the iPhone's general settings. 起初,我认为这是因为他们必须在iPhone的常规设置中打开该应用程序的Facebook隐私设置。 However, because so many are having the same issue, I am beginning to wonder if it's because of an error in my code (see below)? 但是,由于很多人都遇到相同的问题,所以我开始怀疑这是否是由于我的代码错误(请参见下文)? I followed the demo example and Facebook instructions, but perhaps I missed something? 我遵循了演示示例和Facebook的说明,但是也许我错过了一些东西? Thanks in advance for any help you can offer! 在此先感谢您提供的任何帮助!

CODE: 码:

(In ViewController holding FB button): (在ViewController中,按住FB按钮):

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

(In AppDelegate.m): (在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;
    }
}
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 在openActiveSessionWithReadPermissions块中尝试此代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM