使用最新的 Facebook SDK 无法登录 work/stuck

Login doesn't work/stuck with Latest Facebook SDK

我是运行使用 Facebook SDK(4.9.1) 的问题,如果没有系统帐户,它会在应用程序浏览器中打开以进行登录。

从系统 Facebook 帐户登录效果很好。我正在使用以下代码

FBSDKLoginManager *manager = [[FBSDKLoginManager alloc] init];
   manager.loginBehavior = FBSDKLoginBehaviorSystemAccount;
   [manager logInWithReadPermissions:@[@"public_profile",@"email",@"user_friends"] fromViewController:controller handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
            if (error) {
                success(kFacebookStatusFailed);
            } else if (result.isCancelled) {
                success(kFacebookStatusCancelled);
            } else {
                success(kFacebookStatusSuccess);
            }
}];

我已正确完成所有必需的 info.plist 设置 here

问题是使用inapp浏览器登录时,实际使用的是调试发现的SafariViewController。它经历了所有的登录过程,但最后它停留在一个白色的空白屏幕并且没有关闭,请参见下面的屏幕截图

已尝试所有可能的步骤,但无法克服它。但是当我设置新项目时,只有上面的登录代码和适当的设置,一切正常。

Fresh Project 和 Original Project 的唯一区别是应用中使用的不同框架的数量,例如 CrashlyticsFabricInstabugGoogle Frameworks 等。有什么理由让我怀疑这些框架导致了问题,我认为没有。那么,可能是什么问题,任何遇到同样问题并能帮助我的人将不胜感激。

谢谢。

是 登录有问题

-(IBAction)fbAction:(id)sender

{

    FBSDKLoginManager *manager = [[FBSDKLoginManager alloc] init];
     manager.loginBehavior=FBSDKLoginBehaviorWeb;
    
    if ([FBSDKAccessToken currentAccessToken])
    {
        [manager logOut];
        NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
        
        [self fetchUserInfo];
    }
    else
    {
        [manager logInWithReadPermissions:@[@"email"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
         {
             if (error)
             {
                 NSLog(@"Login process error");
             }
             else if (result.isCancelled)
             {
                 NSLog(@"User cancelled login");
             }
             else
             {
                 NSLog(@"Login Success");
                 if ([result.grantedPermissions containsObject:@"email"])
                 {
                     NSLog(@"result is:%@",result);
                     [self fetchUserInfo];
                     
                 }
             }
         }];
    }

}

-(void)fetchUserInfo

{

        if ([FBSDKAccessToken currentAccessToken])
        {
            NSLog(@"Token is available : %@",[[FBSDKAccessToken currentAccessToken]tokenString]);
            
            [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, email"}]
             //[[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, first_name, last_name, picture.type(large), email"}]
             
             startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                 if (!error)
                 {
                     NSLog(@"results:%@",result);
                     
                     NSString *email = [result objectForKey:@"email"];
                     NSString *userId = [result objectForKey:@"id"];
                     
                     if (email.length >0 )
                     {
                         
                         NSLog(@"email %@,userid %@",email,userId);
                     }
                     else
                     {
                         NSLog(@"Facebook email is not verified");
                     }
                 }
                 else
                 {
                     NSLog(@"Error %@",error);
                 }
             }];
        }
    
}

对我来说,我安装了 Branch SDK,但我的 openURL: 应用程序委托方法被搞砸了。

- (BOOL)application:(UIApplication *)application openURL:(nonnull NSURL *)url options:(nonnull NSDictionary <NSString *, id> *)options

仅打开 Branch 个链接,而

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

正在打开 FacebookBranch 链接。

仔细检查两种方法(iOS9 和 <=iOS8)中的逻辑是否设置正确。

我遇到了同样的问题。将此添加到您的 AppDelegate:

在AppDelegate.m

  1. application:didFinishLaunchingWithOptions:

    -(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
        [[FBSDKApplicationDelegate sharedInstance] application:application
                             didFinishLaunchingWithOptions:launchOptions];
    }
    
  2. 将以下方法添加到您的 AppDelegate.m:

    -(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
      return [[FBSDKApplicationDelegate sharedInstance] application:application openURL:url sourceApplication:sourceApplication annotation:annotation];
    }
    

我不知道 SafariViewController 是否检查 application:openURL:sourceApplication:annotation 但这对我有用。 试试看。希望对你有用。

来源:https://developers.facebook.com/docs/ios/getting-started

抱歉,我没有 "speak" objective C,但您必须覆盖 OpenUrl。 这是 C#:

public override bool OpenUrl(UIApplication application, NSUrl url, string sourceApplication, NSObject annotation)
    {
        // Facebook - We need to handle URLs by passing them to their own OpenUrl in order to make the SSO authentication works.
        return ApplicationDelegate.SharedInstance.OpenUrl(application, url, sourceApplication, annotation);
    }