我正在尝试使用适用于 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。
有人可以告诉我该怎么做吗?
谢谢