使用Facebook SDK在iOS 10上登录,共享和使用Facebook SDK时获取空白页4.15.1

问题描述:

使用FBSDKLoginKit,FBSDKShareKit登录,共享链接和我的应用程序中的链接时出现此错误。我用FBSDKLoginButton登录使用Facebook SDK在iOS 10上登录,共享和使用Facebook SDK时获取空白页4.15.1

@property (nonatomic, strong) IBOutlet FBSDKLoginButton *loginButton; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.loginButton.publishPermissions = @[@"publish_actions"]; 
} 

使用FBSDKShareKit分享 :

- (FBSDKShareDialog *)getShareDialogWithContentURL:(FBSDKShareLinkContent *)content 
{ 
    FBSDKShareDialog *shareDialog = [[FBSDKShareDialog alloc] init]; 
    shareDialog.shareContent = content; 
    return shareDialog; 
} 

- (IBAction)ShareAppOnFB:(UIButton *)sender { 

     FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init]; 
     FBSDKShareDialog *facebookShareDialog = [self getShareDialogWithContentURL:content]; 

     if ([facebookShareDialog canShow]) { 
      [FBSDKShareDialog showFromViewController:self.parentViewController 
             withContent:content 
              delegate:self]; 
     } 

} 

使用FBSDKShareKit喜欢:

FBSDKLikeButton *like = [[FBSDKLikeButton alloc] init]; 
like.objectID = @""; 
like.frame = CGRectOffset(like.frame, 50, 100); 
[self.view addSubview:like]; 

一切工作正常iOS上的9和iOS 8,但是当我升级到Xcode 8并在iOS 10上运行,当点击登录,共享和类似按钮时,我立即得到一个空白页面,之后什么也没有发生。我试图升级到Facebook SDK 4.15.1,但没有更好的,这个错误仍然发生。任何人都知道如何解决iOS 10上的这个错误?

FB SDK使用SFSafariViewController,显然在iOS 10中,它只能从根视图控制器呈现。 的解决方案是创建一个新的UIWindow实例,添加一个普通的UIViewController为根视图控制器,并调用FB SDK与这个新的UIViewController:

UIViewController* socialVC = [[UIViewController alloc] init]; 
// window instance needs to be retained 
self.socialWindow = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 
self.socialWindow.rootViewController = socialVC; 
// show new window in top of every other UIs 
self.socialWindow.windowLevel = UIWindowLevelStatusBar + 10; 
[self.socialWindow makeKeyAndVisible]; 

// show FB Share Dialog 
[FBSDKShareDialog showFromViewController:socialVC withContent:content delegate:self]; 

不要忘了隐藏在FB SDK的委托被称为窗口:

self.socialWindow.hidden = YES; 
+1

创建一个新的UIWindow实例非常激进。您应该构建应用程序,以便视图控制器呈现链接回到根视图控制器 - 例如是一个子视图控制器。 – Tim