我正在尝试使用iOS版Facebook SDK将Facebook登录名添加到我的应用程序中。
因为对Facebook服务器的请求可能需要一些时间,所以我认为使用MBProgressHUD。
问题是MBProgressHUD和FBRequest都使用块,因此我期望一些奇怪的行为。
这是我使用的代码:
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:
在块完成后立即关闭HUD。 facebook身份验证方法在后台执行代码并立即返回。相反,请尝试仅显示HUD,并将其隐藏在Facebook完成块中。
-(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.];
}