[英]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
方法的内 FBRequest
块populateUserDetails
与其说这是在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.