Facebook Login - iOS 9 - Without Safari

2019-01-08 16:56发布

问题:

It worked fine to login with logInWithReadPermissions in iOS 8, but as soon after the update to iOS 9 it keeps opening Safari or a modal UIWebView.

Does anyone have the same issue?

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login setLoginBehavior:FBSDKLoginBehaviorNative];
[login logInWithReadPermissions:@[@"public_profile",@"email"] fromViewController:nil handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
    NSLog(@"");
}];

回答1:

Seems there's no way to get the previous behavior using newests FB SDKs in iOS9+. You can use an old SDK to do that. Here's a response from the facebook to that question:



回答2:

From now You can change loginBehavior to FBSDKLoginBehaviorSystemAccount, so FBSDKLoginManager gets Facebook logged in user from iOS Facebook Settings ;)

FBSDKLoginManager *fbLogin = [[FBSDKLoginManager alloc] init];
fbLogin.loginBehavior = FBSDKLoginBehaviorSystemAccount;

From FBSDKLoginManager:

Declaration: FBSDKLoginBehaviorSystemAccount
Description: If the account is not available to the app (either not configured by user or as determined by the SDK) this behavior falls back to FBSDKLoginBehaviorNative.
Note: If the account is not available to the app (either not configured by user or as determined by the SDK) this behavior falls back to FBSDKLoginBehaviorNative.



回答3:

Using 20150708 solved it for me in iOS 9.

Link to SDK: https://developers.facebook.com/resources/FacebookSDKs-iOS-20150708.pkg



回答4:

If you want to use the latest SDK and also the native login behaviour on iOS 9

Edit FBSDKServerConfiguration.m's function to always return true

- (BOOL)useNativeDialogForDialogName:(NSString *)dialogName
{
  return @YES;
}

Falling back to Safari still work if the Facebook app isn't installed



回答5:

I noticed that in both changelogs, as well:

Facebook SDK for iOS Changelog v4.x

(v4.6.0 - September 10, 2015) In addition, the SDK dialogs such as Login, Like, Share Dialogs automatically determine the best UI based on the device, including SFSafariViewController instead of Safari. Follow the our Preparing for iOS 9 guide.

Facebook SDK for iOS Changelog v3.x

(v3.24.0 - September 10th 2015) In addition, the SDK dialogs such as Login, Like, Share Dialogs automatically determine the best UI based on the device, including SFSafariViewController instead of Safari. Follow the our Preparing for iOS 9 guide.



回答6:

I followed the doc here: https://developers.facebook.com/docs/ios/ios9

after adding point 2 and 3, my app switches to native facebook app for login...



回答7:

if you want a modal to open up and ask for facebook credentials seperately from Safari or Facebook app, just use the latest facebook sdk and set the login behaviour

FBSDKLoginManager *fbLogin = [[FBSDKLoginManager alloc] init];
[login setLoginBehavior:FBSDKLoginBehaviorWeb];

this will make the logout process more convenient and less confusing for users.



回答8:

Using this version with CocoaPods:

pod 'FBSDKCoreKit', '4.4.0'
pod 'FBSDKLoginKit', '4.4.0'
pod 'FBSDKShareKit', '4.4.0'


回答9:

In swift When your select login behavior is Web, Then open webView popUp. There is no login redirect to safari browser

// Try to login with permissions
    let fbLoginManager : FBSDKLoginManager = FBSDKLoginManager()

    fbLoginManager.loginBehavior = FBSDKLoginBehavior.Web


回答10:

You want to change the default login behavior as follows

FBSDKLoginManager *login = [[FBSDKLoginManager alloc] init];
[login logOut];

login.loginBehavior = FBSDKLoginBehaviorSystemAccount;

[login logInWithReadPermissions:@[@"public_profile", @"email", @"gender",@"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error)
{
    if (error)
    {
        // Process error
    }
    else if (result.isCancelled)
    {
        // Handle cancellations
    }
    else
    {       
        // Now that we are authorized to view public profile and email, request that data get data from 'me'
        [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields" : @"first_name, last_name,email,name,gender,birthday"}] startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error)
        {
        }];
    }
 }];

if the user does not have their facebook username & password filled out under the Settings app, then safari will pop up.



回答11:

SOLUTION! iOS 9.2.1 Facebook SDK 4.10.0

  1. Install Facebook SDK via Cocoapods

  2. Set:

    FBSDKLoginManager *loginManager = [[FBSDKLoginManager alloc] init]; loginManager.loginBehavior = FBSDKLoginBehaviorNative;

  3. In ~/FBSDKCoreKit/FBSDKServerConfigurationManager.m

Change:

NSOperatingSystemVersion iOS9Version = { .majorVersion = 9, .minorVersion = 0, .patchVersion = 0 };
BOOL useNativeFlow = ![FBSDKInternalUtility isOSRunTimeVersionAtLeast:iOS9Version];

To:

BOOL useNativeFlow = YES;
  1. In ~/FBSDKLoginKit/FBSDKLoginManager.m

Change:

BOOL useNativeDialog = [serverConfiguration useNativeDialogForDialogName:FBSDKDialogConfigurationNameLogin];

To:

BOOL useNativeDialog = YES;
  1. Find in Pods:

    BOOL useNativeDialog

& make the same changes if you want to use other features. For example: App Invite.

Now app uses native Facebook app to log in.

This solution works for me. Hope it helps.

P.S. Don't forget to make changes after upgrading FB SDK.