Search code examples
iosiphoneobjective-cfacebookfacebook-ios-sdk

iOS facebook login via custom view (not via external browser)


I have special requirement of client that he want to be able to login to Facebook from within the app itself, i mean he don't want app to open an external browser where user enters his details, he just want to go with native iOS view having username/mobile and password field.

I have already tried googling but could not find any solution so just want to know is it possible to do and if yes then how?

Thanks in advance.

Regards, Mahesh.


Solution

  • Use following method :

    -(void)openFacebookAuthentication
    {
        NSArray *permission = [NSArray arrayWithObjects:kFBEmailPermission,kFBUserPhotosPermission, nil];
    
        FBSession *session = [[FBSession alloc] initWithPermissions:permission];
    
        [FBSession setActiveSession: [[FBSession alloc] initWithPermissions:permission] ];
    
        [[FBSession activeSession] openWithBehavior:FBSessionLoginBehaviorForcingWebView completionHandler:^(FBSession *session, FBSessionState status, NSError *error) {
    
            switch (status) {
                case FBSessionStateOpen:
                    [self getMyData];
                    break;
                case FBSessionStateClosedLoginFailed: {
                    // prefer to keep decls near to their use
                    // unpack the error code and reason in order to compute cancel bool
                    NSString *errorCode = [[error userInfo] objectForKey:FBErrorLoginFailedOriginalErrorCode];
                    NSString *errorReason = [[error userInfo] objectForKey:FBErrorLoginFailedReason];
                    BOOL userDidCancel = !errorCode && (!errorReason || [errorReason isEqualToString:FBErrorLoginFailedReasonInlineCancelledValue]);
    
    
                    if(error.code == 2 && ![errorReason isEqualToString:kFBSdkUserLoginFail]) {
                        UIAlertView *errorMessage = [[UIAlertView alloc] initWithTitle:kFBAlertTitle
                                                                               message:kFBAuthenticationErrorMessage
                                                                               delegate:nil
                                                                               cancelButtonTitle:kOk
                                                                               otherButtonTitles:nil];
                        [errorMessage performSelectorOnMainThread:@selector(show) withObject:nil waitUntilDone:YES];
                        errorMessage = nil;
                        }
                    }
                    break;
                    // presently extension, log-out and invalidation are being implemented in the Facebook class
                default:
                    break; // so we do nothing in response to those state transitions
            }
        }];
        permission = nil;
    }
    

    Here is my Post : Login with facebook in iPhone without redirecting to the web browser?