Login with facebook in iPhone without redirecting to the web browser?

我的未来我决定 提交于 2019-12-13 04:50:29

问题


I have used login with Facebook in my app and its working well I am using Facebook sdk 3.10 , but it just redirects me to the web browser and open the Facebook login page pr permission if there is already a session exists, but I don't want in this way I want the a login popup of Facebook like in previous sdks should open and there I fill the credentials my code is below please what can I change in this to do the same.

NSArray *permissions =[NSArray arrayWithObjects:@"email",@"user_location",@"user_about_me",@"user_photos",@"xmpp_login", nil];
NSHTTPCookie *cookie;
NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
for (cookie in [storage cookies])
{
    [storage deleteCookie:cookie];
}
[[NSUserDefaults standardUserDefaults] synchronize];
[FBSession openActiveSessionWithReadPermissions:permissions
                                   allowLoginUI:YES
                              completionHandler:^(FBSession *session,
                                                  FBSessionState status,
                                                  NSError *error)
 {
     // if login fails for any reason, we alert
     if (error) {
         UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
                                                         message:error.localizedDescription
                                                        delegate:nil
                                               cancelButtonTitle:@"OK"
                                               otherButtonTitles:nil];
         [alert show];
     }
     else if (FB_ISSESSIONOPENWITHSTATE(status))
     {
         FBRequest *me = [[FBRequest alloc] initWithSession:session
                                                  graphPath:@"me?fields=id,picture.type(large),first_name,last_name,email,gender,birthday,username,hometown"];
         [me startWithCompletionHandler:^(FBRequestConnection *connection,
                                          NSDictionary<FBGraphUser> *aUser,
                                          NSError *error)
         {
             fbDetails = [[NSDictionary alloc]init];
             fbDetails=[aUser copy];
             facebookDetail = [appParser getUserFbDetail:fbDetails];

         }];
     }
 }];

回答1:


Use Following method to open FBLogin popup

-(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;
}


来源:https://stackoverflow.com/questions/20696766/login-with-facebook-in-iphone-without-redirecting-to-the-web-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!