简体   繁体   English

使用facebook登录IOS延迟查找用户的电子邮件

[英]Delay in finding user's email with facebook login IOS

The request and login occurs all fine but with delay that disrupts what I want to do. 请求和登录发生得很好但延迟会破坏我想要做的事情。

When the user taps startButton I call the facebook login method and that occurs fine, session is open and then call 'populateUserDetails' to get the user's email that information comes with a delay that makes my variables name and email goes empty to my service because the signIn method is called before the user's email and name from populateUserDetails arrive. 当用户点击startButton我调用facebook登录方法并且发生正常,会话打开然后调用'populateUserDetails'来获取用户的电子邮件,该信息带有延迟,使我的变量名称和电子邮件变为我的服务,因为在用户的电子邮件和populateUserDetails的名称到达之前调用signIn方法。

Login button action and facebook methods: 登录按钮动作和facebook方法:

- (IBAction)actionButtonStart:(id)sender
{
    if (FBSession.activeSession.state == FBSessionStateOpen
        || FBSession.activeSession.state == FBSessionStateOpenTokenExtended) {

        [FBSession.activeSession closeAndClearTokenInformation];

    } else {

        [FBSession openActiveSessionWithReadPermissions:@[@"basic_info"]
                                           allowLoginUI:YES
                                      completionHandler:
         ^(FBSession *session, FBSessionState state, NSError *error) {

             [self sessionStateChanged:session state:state error:error];
         }];
    }
}

- (void)populateUserDetails
{
    if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,
           NSError *error) {
             if (!error) {
                 NSLog(@"%@", user.name);
                 NSLog(@"%@", [user objectForKey:@"email"]);
                 self.nome = user.name;
                 self.email = [user objectForKey:@"email"];
             }
         }];
    }
}

- (void)sessionStateChanged:(FBSession *)session state:(FBSessionState) state error:(NSError *)error
{
    if (!error && state == FBSessionStateOpen){
        NSLog(@"Session opened");

        [self populateUserDetails];
        [self signIn];

        return;
    }
    if (state == FBSessionStateClosed || state == FBSessionStateClosedLoginFailed){
        NSLog(@"Session closed");
    }
    if (error){
        NSLog(@"Error");
        NSString *alertText;
        NSString *alertTitle;

        if ([FBErrorUtility shouldNotifyUserForError:error] == YES){

            alertTitle = @"Something went wrong";
            alertText = [FBErrorUtility userMessageForError:error];
            //[self showMessage:alertText withTitle:alertTitle];
        } else {

            if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
                NSLog(@"User cancelled login");

            } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession){
                alertTitle = @"Session Error";
                alertText = @"Your current session is no longer valid. Please log in again.";
                //[self showMessage:alertText withTitle:alertTitle];

              https://developers.facebook.com/docs/ios/errors
            } else {
                //Get more error information from the error
                NSDictionary *errorInformation = [[[error.userInfo objectForKey:@"com.facebook.sdk:ParsedJSONResponseKey"] objectForKey:@"body"] objectForKey:@"error"];

                alertTitle = @"Something went wrong";
                alertText = [NSString stringWithFormat:@"Please retry. \n\n If the problem persists contact us and mention this error code: %@", [errorInformation objectForKey:@"message"]];
                //[self showMessage:alertText withTitle:alertTitle];
            }
        }

        [FBSession.activeSession closeAndClearTokenInformation];
        //[self userLoggedOut];
    }
}

- (void)loginView:(FBLoginView *)loginView handleError:(NSError *)error {
    NSString *alertMessage, *alertTitle;

    if ([FBErrorUtility shouldNotifyUserForError:error]) {
        alertTitle = @"Facebook error";
        alertMessage = [FBErrorUtility userMessageForError:error];

    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryAuthenticationReopenSession) {
        alertTitle = @"Session Error";
        alertMessage = @"Your current session is no longer valid. Please log in again.";

    } else if ([FBErrorUtility errorCategoryForError:error] == FBErrorCategoryUserCancelled) {
        NSLog(@"user cancelled login");

    } else {
        alertTitle  = @"Something went wrong";
        alertMessage = @"Please try again later.";
        NSLog(@"Unexpected error:%@", error);
    }

    if (alertMessage) {
        [[[UIAlertView alloc] initWithTitle:alertTitle
                                    message:alertMessage
                                   delegate:nil
                          cancelButtonTitle:@"OK"
                          otherButtonTitles:nil] show];
    }
}

Sign in method 登录方法

- (void)signIn
{

    if([GenericService checkConnection]){

        GenericService *service = [[GenericService alloc] initWithDelegate:self andCallback:@selector(answerSignIn:)];
        service.metodo = 1;
        service.messageLoading = @"Wait...";
        service.url = @"http://myservice.com/signIn.json";
        [service addParameter:self.name withKey:@"name"];
        [service addParameter:self.email withKey:@"email"];
        [service request];

    }
}

- (NSString *) answerSignIn:(NSDictionary *)answer {

    NSLog(@"%@", [answer description]);

    NSString *sucess = [answer objectForKey:@"sucesso"];

    if (sucess)
        [self.navigationController pushViewController:self.tabBarController animated:YES];

    return sucess;
}

This is happening because the FBRequest block is asynchronous, ie it performs the FBRequest in the background, as to allow your app to continue other processes while it's busy fetching the information. 发生这种情况是因为FBRequest块是异步的,即它在后台执行FBRequest,以允许您的应用程序在忙于获取信息时继续其他进程。 So yes, there will be a delay, but in order to still have the info you need when you need it, call the signIn method within your FBRequest block in populateUserDetails instead of calling it in sessionStateChanged:state:error: , like so: 所以,是的,会有一个延迟,但为了仍然有你需要的时候你需要它,调用的信息signIn方法的 FBRequestpopulateUserDetails与其说这是在sessionStateChanged:state:error: ,就像这样:

- (void)populateUserDetails
{
    if (FBSession.activeSession.isOpen) {
        [[FBRequest requestForMe] startWithCompletionHandler:
         ^(FBRequestConnection *connection,
           NSDictionary<FBGraphUser> *user,
           NSError *error) {
             if (!error) {
                 NSLog(@"%@", user.name);
                 NSLog(@"%@", [user objectForKey:@"email"]);
                 self.nome = user.name;
                 self.email = [user objectForKey:@"email"];

                 [self signIn];
             }
         }];
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Facebook登录并获取iOS 7的电子邮件和用户信息 - Login with Facebook and fetch email and user info for iOS 7 检索用户的电子邮件Google Login iOS - Retrieving user's email Google Login iOS 如何在iOS上使用Facebook SDK 3.1获取用户的电子邮件? - How to get user's email with Facebook SDK 3.1 on iOS? 无法从iOS 8中的Facebook获取用户的电子邮件个人信息 - not Getting user's email Personal info from Facebook in iOS 8 Parse.com是否可以使用facebook登录,同时检查Facebook用户的电子邮件是否存在于解析用户中,以及是否链接两者? - Parse.com Is it possible to login using facebook while checking if the facebook user's email exists in a user in parse and if it does linking both? ios Facebook登录,某些Facebook帐户未返回电子邮件 - ios facebook login , email not returning for some facebook accounts Facebook iOS SDK不返回用户电子邮件 - Facebook iOS SDK not returning user email 当用户在 ios 13.0 中单击登录按钮时,Facebook 登录总是被取消,但它在 ios 12.0 或更小的 swift 中完全正常工作 - Facebook login is always cancel when user click on login button in ios 13.0 but it's totally working fine in ios 12.0 or smaller in swift Facebook iOS SDK用户配置文件登录属性 - Facebook iOS SDK User Profile Login Attributes 在 iOS 版 Facebook SDK 中登录不同的用户 - Login different user in Facebook SDK for iOS
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM