Get blank page while log in, share and like with Facebook SDK on iOS 10 with Facebook SDK 4.15.1
Asked Answered
S

1

3

I get this error when using FBSDKLoginKit, FBSDKShareKit, to login, share link and like a link in my application. I using FBSDKLoginButton to login

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

- (void)viewDidLoad {
    [super viewDidLoad];

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

Using FBSDKShareKit to share:

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

}

Using FBSDKShareKit to Like:

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

Everything work fine on iOS 9 and iOS 8, but when I upgrade into Xcode 8 and run on iOS 10, I get a blank page right away when tap on Login, Share and Like Button, and nothing happens after that. I tried to upgrade to Facebook SDK 4.15.1 but nothing better, this bug still happens. Anyone know how to fix this bug on iOS 10?

Shed answered 17/9, 2016 at 8:43 Comment(0)
N
6

FB SDK uses SFSafariViewController, and apparently in iOS 10 it can be presented only from root view controllers. The solution is to create a new UIWindow instance, add a plain UIViewController as root view controller, and call FB SDK with this new 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];

Do not forget to hide the window when FB SDK's delegate is called:

self.socialWindow.hidden = YES;
Nocturne answered 30/9, 2016 at 20:24 Comment(1)
Creating a new UIWindow instance is pretty aggressive. You should just structure your application so that the view controller presenting has a link back to the root view controller - e.g. is a child view controller.Uncouple

© 2022 - 2024 — McMap. All rights reserved.