gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:49:20 24 4
gpt4 key购买 nike

请求和登录一切正常,但延迟扰乱了我想做的事情。

当用户点击 startButton 时,我调用 facebook 登录方法并且发生正常, session 打开,然后调用“populateUserDetails”以获取用户的电子邮件,该信息带有延迟,使我的变量名称和电子邮件为空服务,因为 signIn 方法在来自 populateUserDetails 的用户电子邮件和姓名到达之前被调用。

登录按钮操作和 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];
}
}

登录方式

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

最佳答案

发生这种情况是因为 FBRequest block 是异步的,即它在后台执行 FBRequest,以允许您的应用在忙于获取信息时继续其他进程。所以是的,会有延迟,但为了在需要时仍能获得所需信息,请调用 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];
}
}];
}
}

关于ios - 使用facebook登录IOS延迟查找用户的电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20817241/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com