iOS 9 Facebook SDK Login/Share

2019-02-04 23:13发布

问题:

I have run into an issue using the latest Facebook SDK (4.6) on iOS 9. To authorize a user, I use the FBSDKLoginManager. I have tried setting this:

loginManager.loginBehavior = FBSDKLoginBehaviorNative;

But the SDK still seems to always use an SFSafariViewController to authorize Facebook.

Here is the problem I am facing. If a user has the FB app installed on their device with FacebookAccountA, then tries to authenticate with my app, they are presented this SFSafariViewController and they can authenticate the app with FacebookAccountB (a separate FB account).

Then in the app, I try to share with the following:

FBSDKShareLinkContent *content = [[FBSDKShareLinkContent alloc] init];
content.contentURL = [NSURL URLWithString:shareString];

FBSDKShareDialog *dialog = [[FBSDKShareDialog alloc] init];
dialog.fromViewController = self;
dialog.shareContent = content;
dialog.delegate = self;
[dialog show];

The default setting for the share dialog ends up being the FBSDKShareDialogModeShareSheet. The share sheet presents in the app correctly, and the post seems to work, however the link is posted to FacebookAccountA (the native app account), NOT FacebookAccountB (the one that authenticated with my app).

So, if a user happens to authenticate my app with a different facebook account than they are signed into on their native app, they can end up posting to an account that was different than the one they used to authenticate my app. Is there a way to prevent this?

Thanks in advance.

回答1:

This is by design.

In FB SDK v4.6 and v3.24, we default to Safari View Controller rather than fast-app-switching to the native Facebook app. In iOS 9 the fast-app-switch flow generates two interstitials "ExampleApp would like to open Facebook" and "Facebook would like to open ExampleApp".

For the hundreds of millions of people who are signed into Safari on iOS, they have an awesome experience.



回答2:

Use SLComposerViewController for Fb sharing. It will use your ios device default Facebook sdks login or your installed application login.

Below is the reference for this: Tutorial for SLComposeViewController sharing

Hope this helps!



回答3:

Yes, it is by design on iOS 9, but we can easily work around it!

let dialog = FBSDKShareDialog()
dialog.mode = .Native
if !dialog.canShow() {
    self.mode = .Automatic
}

Yay!