Search code examples
iosfacebookfacebook-graph-apifacebook-ios-sdk

FaceBook API, login in-app


I followed this guide and I've created my app successfully with Facebook integration.

What's the problem?

When the user has to do the login, the app quits in the browser (or in Facebook app, if is installed)

How do I keep authentication entirely in-app?


Solution

  • The point of the oAuth login is that it doesn't happen within your application. It uses fast-app switching to perform the authentication in a trusted environment (either Safari or the Facebook application).

    However, you can modify Facebook.m to do the authentication within your application, but you user's credentials will not be remembered. You can see that if your iOS device doesn't support multi-tasking, there is a backup login dialog.

    Excerpt from Facebook.m (around line 160):

    if ([device respondsToSelector:@selector(isMultitaskingSupported)] && [device isMultitaskingSupported]) {
        if (tryFBAppAuth) {
          NSString *scheme = kFBAppAuthURLScheme;
          if (_localAppId) {
            scheme = [scheme stringByAppendingString:@"2"];
          }
          NSString *urlPrefix = [NSString stringWithFormat:@"%@://%@", scheme, kFBAppAuthURLPath];
          NSString *fbAppUrl = [FBRequest serializeURL:urlPrefix params:params];
          didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
        }
    
        if (trySafariAuth && !didOpenOtherApp) {
          NSString *nextUrl = [self getOwnBaseUrl];
          [params setValue:nextUrl forKey:@"redirect_uri"];
    
          NSString *fbAppUrl = [FBRequest serializeURL:loginDialogURL params:params];
          didOpenOtherApp = [[UIApplication sharedApplication] openURL:[NSURL URLWithString:fbAppUrl]];
        }
    }
    
    // If single sign-on failed, open an inline login dialog. This will require the user to
    // enter his or her credentials
    if (!didOpenOtherApp) {
        [_loginDialog release];
        _loginDialog = [[FBLoginDialog alloc] initWithURL:loginDialogURL
                                          loginParams:params
                                             delegate:self];
        [_loginDialog show];
    }
    

    If you remove the first conditional and it's containing code and set didOpenOtherApp to NO, you can get the behavior you are looking for.