0

我正在尝试使用适用于 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。

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

谢谢

4

1 回答 1

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.];
}
于 2013-06-15T00:44:40.517 回答