Search code examples
iosfacebookfacebook-ios-sdk

iOS Facebook login doesn't work


I have an app, for iOS5 and above, in which I am trying to set up Facebook login. All works fine when there is a Facebook account integrated in the phone. If it is not, it doesn't work. This is even if the Facebook app is installed.

When the app is installed, it opens the app, asks me permission to access the appropriate user data and returns to my app with no result. It the same when it isn't installed. Safari is opened, asks me for authorization, returns, and nothing.

I followed all the steps mentioned in the Facebook Login tutorial. Is this a problem only I am facing?

EDIT: This is the requested code:

I open a session like so:

- (BOOL)openSessionWithAllowLoginUI:(BOOL)allowLoginUI {

    NSArray *permissions = [[NSArray alloc] initWithObjects:
                        @"email",
                        nil];

    return [FBSession openActiveSessionWithReadPermissions:permissions
                                          allowLoginUI:allowLoginUI
                                     completionHandler:^(FBSession *session,
                                                         FBSessionState state,
                                                         NSError *error) {
                                         [self sessionStateChanged:session
                                                             state:state
                                                             error:error];
                                     }];
}

And when it returns, I guess this method must be called. but it isn't:

- (void)sessionStateChanged:(FBSession *)session
                  state:(FBSessionState) state
                  error:(NSError *)error
{

    switch (state) {
        case FBSessionStateOpen:
            if (!error) {
                // We have a valid session
                NSLog(@"session opened");
                FBRequest *me = [FBRequest requestForMe];
                __block NSString *username = nil;

                [me startWithCompletionHandler:^(FBRequestConnection *connection,
                                             id result,
                                             NSError *error) {
                    NSDictionary<FBGraphUser> *my = (NSDictionary<FBGraphUser> *) result;
                  //  NSLog(@"User session found: %@", my.username);
                    username = my.username;
                }];

            // /fb_access/:access_token

        }
        break;
    case FBSessionStateClosed:

        if (!error) {
            // We have a valid session
            NSLog(@"User session closed");

        }

    case FBSessionStateClosedLoginFailed:
         NSLog(@"login failed, %@", error);
        [FBSession.activeSession closeAndClearTokenInformation];
        break;
    default:
        break;
}

[[NSNotificationCenter defaultCenter]
 postNotificationName:FBSessionStateChangedNotification
 object:session];

if (error) {
    UIAlertView *alertView = [[UIAlertView alloc]
                              initWithTitle:@"Error"
                              message:error.localizedDescription
                              delegate:nil
                              cancelButtonTitle:@"OK"
                              otherButtonTitles:nil];
    [alertView show];
}
}

Any ideas?


Solution

  • Add following method in to your appdelgate

     - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
    
     { // attempt to extract a token from the url return
    
     [FBSession.activeSession handleOpenURL:url]; 
    
     }