Facebook native login is not opening facebook app even if it's installed in device?

扶醉桌前 提交于 2020-01-23 06:42:38

问题


i have followed every step described in the docs of facebook-iso-sdk 4.8.0 for iOS 9, but still couldn't preform app switch on "login-with-facebook" in my app, even if facebook app is already installed.

As you can see in screen shot below i have modified info.plist, but still can't get native app switch to work.

I have also double checked for typo-mistakes in info.plist value. and i can assure you they are correct.

Here is my code :-

    if (![AppDelegate SharedInstance].login) {
        [AppDelegate SharedInstance].login = [[FBSDKLoginManager alloc] init];
    }
    [AppDelegate SharedInstance].login.loginBehavior = FBSDKLoginBehaviorNative;
    [[AppDelegate SharedInstance].login logInWithReadPermissions:@[@"public_profile",@"email",@"user_friends"] fromViewController:self handler:^(FBSDKLoginManagerLoginResult *result, NSError *error) {
        if (error) {

        }
        else if (result.isCancelled)
        {
            // Handle cancellations
        }
        else
        {
            NSLog(@"result.grantedPermissions == %@",result.grantedPermissions);
            if (result.token)
            {
                [[[FBSDKGraphRequest alloc] initWithGraphPath:@"me" parameters:@{@"fields": @"id, name, email, first_name, last_name"}]
                 startWithCompletionHandler:^(FBSDKGraphRequestConnection *connection, id result, NSError *error) {
                     if (!error) {
                         NSLog(@"fetched user:%@", result);
                         NSString *userImageURL = [NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", [result objectForKey:@"id"]];
                         [dictFacebookDetail addEntriesFromDictionary:result];
                         [dictFacebookDetail setObject:userImageURL forKey:@"profilepic"];
                         NSLog(@"facebook login result --- %@",dictFacebookDetail);
                         [self performSelectorInBackground:@selector(CheckFacebookUser:) withObject:dictFacebookDetail];
                     }
                 }];
            }
        }
    }];

What am i missing ?


回答1:


I have found a solution, but you should change something in FBSDKLogin pod. I was debugging the Pod and I realized that Facebook ask in the class FBSDKServerConfiguration for the server configuration for the app. It returns a JSON with some information to configure the Pod for our app. I realized that by default the JSON returns this dictionary:

 "ios_sdk_dialog_flows" =     {
        default =         {
            "use_native_flow" = 0;
            "use_safari_vc" = 1;
        };
        message =         {
            "use_native_flow" = 1;
        };
    };

By default the use_native_flow is 0, so when it saves the information in userDefaults for the next app launches. So, when the app calls FBSDKLoginMananger login method and checks for the loginBehaviour in this method, the variable useNativeDialog returns NO. So the switch uses the next case. case FBSDKLoginBehaviorBrowser:

- (void)logInWithBehavior:(FBSDKLoginBehavior)loginBehavior
{
.
.
...
  switch (loginBehavior) {
    case FBSDKLoginBehaviorNative: {
      if ([FBSDKInternalUtility isFacebookAppInstalled]) {
        [FBSDKServerConfigurationManager loadServerConfigurationWithCompletionBlock:^(FBSDKServerConfiguration *serverConfiguration, NSError *loadError) {
            BOOL useNativeDialog = [serverConfiguration useNativeDialogForDialogName:FBSDKDialogConfigurationNameLogin];
          if (useNativeDialog && loadError == nil) {
            [self performNativeLogInWithParameters:loginParams handler:^(BOOL openedURL, NSError *openedURLError) {
              if (openedURLError) {
                [FBSDKLogger singleShotLogEntry:FBSDKLoggingBehaviorDeveloperErrors
                                   formatString:@"FBSDKLoginBehaviorNative failed : %@\nTrying FBSDKLoginBehaviorBrowser", openedURLError];
              }
              if (openedURL) {
                completion(YES, FBSDKLoginManagerLoggerAuthMethod_Native, openedURLError);
              } else {
                [self logInWithBehavior:FBSDKLoginBehaviorBrowser];
              }
            }];
          } else {
            [self logInWithBehavior:FBSDKLoginBehaviorBrowser];
          }
        }];
        break;
      }
      // intentional fall through.
    }
    case FBSDKLoginBehaviorBrowser: {
    .
    . 
    . 

}

As we see in the code, we know if the app is installed in this if, if ([FBSDKInternalUtility isFacebookAppInstalled]). To solve the problem, I have changed this line

BOOL useNativeDialog = [serverConfiguration useNativeDialogForDialogName:FBSDKDialogConfigurationNameLogin];

to

 BOOL useNativeDialog = YES;

I know this is not a good practice and it will change if I update this Pod, but at least is working and I needed it now. I guess we can change that configuration in facebook developers admin site, but I haven't found anything.




回答2:


Facebook has changed Facebook login behavior for iOS9.

Here is the quote from Facebook blog post:

We've been monitoring data and CTRs for over 250 apps over the last 6 weeks since iOS 9 launched. The click-through rate (CTR) of SVC Login outperforms the CTR of app-switch Login and is improving at 3x the rate of the app-switch experience. This indicates that the SVC experience is better for people and developers today, and will likely be the best solution in the long run. For this reason, the latest Facebook SDK for iOS uses SVC as the default experience for Login.




回答3:


typedef NS_ENUM(NSUInteger, FBSDKLoginBehavior)
{
  /*!
   @abstract This is the default behavior, and indicates logging in through the native
   Facebook app may be used. The SDK may still use Safari instead.
   */
  FBSDKLoginBehaviorNative = 0,
  /*!
   @abstract Attempts log in through the Safari or SFSafariViewController, if available.
   */
  FBSDKLoginBehaviorBrowser,
  /*!
   @abstract Attempts log in through the Facebook account currently signed in through
   the device Settings.
   @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 \c FBSDKLoginBehaviorNative.
   */
  FBSDKLoginBehaviorSystemAccount,
  /*!
   @abstract Attemps log in through a modal \c UIWebView pop up

   @note This behavior is only available to certain types of apps. Please check the Facebook
   Platform Policy to verify your app meets the restrictions.
   */
  FBSDKLoginBehaviorWeb,
};

there is lot of behaviour available to access the fb login.try using alternate what you prefer from this.

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

Like this ...hope this will help you :)




回答4:


  • (FBSDKServerConfiguration *)_defaultServerConfigurationForAppID:(NSString *)appID { // Use a default configuration while we do not have a configuration back from the server. This allows us to set // the default values for any of the dialog sets or anything else in a centralized location while we are waiting for // the server to respond. static FBSDKServerConfiguration *_defaultServerConfiguration = nil; if (![_defaultServerConfiguration.appID isEqualToString:appID]) { // Bypass the native dialog flow for iOS 9+, as it produces a series of additional confirmation dialogs that lead to // extra friction that is not desirable. NSOperatingSystemVersion iOS9Version = { .majorVersion = 9, .minorVersion = 0, .patchVersion = 0 };


来源:https://stackoverflow.com/questions/33971727/facebook-native-login-is-not-opening-facebook-app-even-if-its-installed-in-devi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!