6

I have implemented FBConnect SSO in my iphone application according to this tutorial. I have tested it on two phones so far, one of which lets me use the function just fine, whereas the other phone simply opens up the Facebook app and closes it right away. I am able to establish a valid session, yet the dialog seems to just close on me on one of the ones.
What could be the problem? Any help is much appreciated.

Tim Stullich
  • 251
  • 6
  • 19
  • this was happening with me too.. check if FBapp id is properly set – iMeMyself Aug 01 '12 at 06:47
  • I'll look into it. I think it has something to do with the fact that one phone has the Facebook app installed and the other doesn't. – Tim Stullich Aug 01 '12 at 06:55
  • Maybe you are using an account that already approved the app's permissions, so the FB app just checks if the user is valid and then goes back to your application. – andreamazz Aug 31 '12 at 08:14
  • In addition to what i've mentioned in the bounty text, also note that this is a case with all sample codes that come with facebookSDK - and I have tested it on multiple devices. The problem is more severe because even if a user is logged into the facebook application, and I try to get authentication - but then on the permissions screen I cancel, the control stays within the facebook app. Does not go back to my application to say the user canceled. – zakishaheen Aug 31 '12 at 09:02
  • Also filed as : https://developers.facebook.com/bugs/361792887232488 – zakishaheen Aug 31 '12 at 09:23

3 Answers3

3

I think that your problem is that you dont have the proper configuration, either in the client side (your app) or in the server side (the FB app configuration). Please, check both things as follows:

Client app:

Make sure that you have introduced the correct FBAppId into your Info.plist as a URL handler. Facebook will try to open the app by using this handler, and if it doesn't work, your app will not be opened.

Plist Config

(replace the XXXXXXXXXX with your FB appID)

Check whether your app (in the appDelegate) handles the custom URL added above:

- (BOOL)handleOpenURL:(NSURL*)url
{
    NSString* scheme = [url scheme];
    NSString* prefix = [NSString stringWithFormat:@"fb%@", facebookAppId];
    if ([scheme hasPrefix:prefix]) {
        return [fb handleOpenURL:url];
    }
    return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation 
{
    return [self handleOpenURL:url];
}

- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url 
{
    return [self handleOpenURL:url];  
}

Server side

Check that you have correctly configured your bundleId in the FB config page. You can check this in https://developers.facebook.com/apps, selecting you app, and then in the edition panel:

FB config

If you have both things correctly configured it should work in any case (with the FBapp installed or Safari based).

Angel G. Olloqui
  • 8,045
  • 3
  • 33
  • 31
  • Thanks for detailed answer, I'll just update that the problem exists with Facebook's sample Scrumptious as well. You can test it yourself on a device. – zakishaheen Sep 07 '12 at 14:28
  • Please see https://developers.facebook.com/bugs/361792887232488?browse=search_504a052eac5980628824152 – zakishaheen Sep 07 '12 at 14:32
3

I have seen this with the 3.0 SDK. The reason it occurs is because of the way facebook caches access tokens. Access tokens are cached both in your app, and in the facebook app on the device (at least conceptually).

If it's the first time you started your app on the device, facebook won't have a valid access token cached in your app. However, it may still have a valid access token in the facebook app for your app. If it finds one on the facebook side, then it doesn't have to ask the user again, so the dialog appears, then disappears, and it sends back the access token. The access token is then cached in your app.

If you restart your app (without deleting it), you won't see the login dialog at all since the access token is now cached in your app. If you delete your app on the device and login again, you'll see the same behavior, as long as facebook can find a cached token on it's side.

I have found a solution to this problem. Here is some sample code:

if (![FBSession activeSession].isOpen) {
    FBSession *session = [[FBSession alloc] initWithAppID:nil permissions:permissions defaultAudience:FBSessionDefaultAudienceEveryone urlSchemeSuffix:nil tokenCacheStrategy:nil];
    [FBSession setActiveSession:session];
    [session openWithBehavior:FBSessionLoginBehaviorUseSystemAccountIfPresent     completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
// NOTE openActiveSessionWithPermissions causes the facebook app to hang with a blank dialog if my app is installed, authed, and then reinstalled. openWithBehavior
// does not. I think it has something to do with the FBSession activeSession.
//        [FBSession openActiveSessionWithPermissions:self.permissions allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
        switch (status) {
            case FBSessionStateOpen:
                [self getFacebookUser];
                break;
            case FBSessionStateClosed:
                [self fbSessionClosed];
                break;
            case FBSessionStateCreated:
                [self fbSessionCreated];
                break;
            case FBSessionStateCreatedOpening:
                [self fbSessionOpening];
                break;
            case FBSessionStateClosedLoginFailed:
                [self fbSessionClosedLoginFailed];
                break;
            case FBSessionStateOpenTokenExtended:
                [self fbSessionOpenTokenExtended];
                break;
            case FBSessionStateCreatedTokenLoaded:
                [self fbSessionCreatedTokenLoaded];
                break;
        }
    }];
}
}

openActiveSessionWithPermissions causes problems, but openWithBehavior does not. openActiveSession makes the same openWithBehavior call, but I think the timing of setting the active session is different, and hence does not work. If I set the active session before calling openWithBehavior, everything seems to work fine. As far as your app and the end user are concerned, everything is identical.

Bill Donahue
  • 136
  • 3
1

You need to insert suitable logging throughout your code, post the code, and post what logs are being made in the working and the failure cases.

To make a log (see here for more advanced techniques):

NSLog(@"My log text");  

At a minimum you need to add logs in all the callbacks from the Facebook API (even the ones you aren't using), and in your handleOpenURL and openUrl methods in the application delegate.

Your logs will appear in the Console in the Xcode Organizer when you connect the device you are testing with - that should show you what is happening.

FYI, the Facebook SDK code will first attempt to use the native app if it is installed, otherwise it will launch the Safari browser to ask for authorization through the web version of Facebook.

You should double check your Facebook URL scheme configuration, as described in the answer by Angel García Olloqui.

Community
  • 1
  • 1
Dan J
  • 25,433
  • 17
  • 100
  • 173
  • I know. The problem is that my app is invoking Facebook API perfectly fine. The login happens, and FB app does not invoke-back my own application. This is true for Facebook Samples as well. – zakishaheen Sep 07 '12 at 14:30