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 ?
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.