gpt4 book ai didi

ios - MBProgressHUD + Facebook SKD : Nested blocks

转载 作者:行者123 更新时间:2023-11-29 03:50:51 24 4
gpt4 key购买 nike

我正在尝试使用适用于 iOS 的 Facebook SDK 将 Facebook 登录添加到我的应用程序。

因为对Facebook服务器的请求可能需要一些时间,所以我想到使用MBProgressHUD。问题是 MBProgressHUD 和 FBRequest 都使用 block ,所以我期待一些奇怪的行为。

这是我使用的代码:

MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];

HUD.delegate = self;
HUD.labelText = @"Loading";
HUD.minSize = CGSizeMake(135.f, 135.f);

[HUD showAnimated:YES whileExecutingBlock:^{

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

if (error) {
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.detailsLabelText = error.localizedFailureReason;
HUD.labelText = @"Error";

}

if (state == FBSessionStateClosedLoginFailed) {
[FBSession.activeSession closeAndClearTokenInformation];
}
if (FBSession.activeSession.isOpen) {



[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {

if (!error) {
//Save User's Data


HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.labelText = @"Logged in";

} else {
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"error"]];
HUD.mode = MBProgressHUDModeCustomView;
HUD.detailsLabelText = @"An error occurred, please retry later";
HUD.labelText = @"Error";
}
}];
}

}];
sleep(2);
} completionBlock:^{

//Return to previous page

}];

问题是,当我按下与此方法相关的按钮时,我会看到进度 HUD 不到一秒钟,然后我会返回到上一页。

我希望看到的是整个过程中HUD的显示。

有人可以告诉我该怎么做吗?

谢谢

最佳答案

问题是 showAnimated:whileExecutingBlock: 在 block 完成后立即关闭 HUD。 facebook 身份验证方法在后台执行代码并立即返回。相反,尝试只显示 HUD,并将其隐藏在 Facebook 完成 block 中。

-(void)yourMethodThatLogsIntoFacebook {
MBProgressHUD *HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];

HUD.delegate = self;
HUD.labelText = @"Loading";
HUD.minSize = CGSizeMake(135.f, 135.f);

[HUD show:YES];

[FBSession :@[@"email"] allowLoginUI:YES completionHandler:^(FBSession *session, FBSessionState state, NSError *error) {
if (error) {
[self updateHud:HUD withImage:@"error" text:@"Error" detailText:error.localizedFailureReason];
}
if (state == FBSessionStateClosedLoginFailed) {
[FBSession.activeSession closeAndClearTokenInformation];
}
if (FBSession.activeSession.isOpen) {
[[FBRequest requestForMe] startWithCompletionHandler:^(FBRequestConnection *connection, NSDictionary<FBGraphUser> *user, NSError *error) {
if (!error) {
//Save User's Data
[self updateHud:HUD withImage:@"37x-Checkmark.png" text:@"Logged in" detailText:nil];
} else {
[self updateHud:HUD withImage:@"error" text:@"Error" detailText:@"An error occurred, please retry later"];
}
}];
}
}];
}

-(void)updateHud:(MBProgressHUD *)hud withImage:(NSString *)imageName text:(NSString *)text detailText:(NSString *)detailText {
hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]];
hud.mode = MBProgressHUDModeCustomView;
hud.labelText = text;
hud.detailText = detailText;
[hud hide:YES afterDelay:2.];
}

关于ios - MBProgressHUD + Facebook SKD : Nested blocks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17092360/

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