2016-09-17 71 views
3

使用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上的這個錯誤?

回答

5

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