我正在尝试使用Facebook SDK for iOS为我的应用添加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。
有人可以告诉我该怎么做吗?
由于
答案 0 :(得分:1)
问题是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.];
}