문제

I'm trying to add a Facebook login to my app with the Facebook SDK for iOS.

Because the request to Facebook's servers may take some time, I thought to use MBProgressHUD. The problem is that both MBProgressHUD and FBRequest use blocks, so I'm expecting some strange behavior.

This is the code i used:

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

}];

The problem is that when I push the button related to this method, I see the progress HUD for less than a second, then I'm brought back to the previous page.

What I'd like to see is the HUD displayed during all the process.

Can someone tell me how to do it?

Thanks

도움이 되었습니까?

해결책

The problem is that showAnimated:whileExecutingBlock: closes the HUD right after the block completes. The facebook authentication method executes code in the background and returns immediately. Instead, try just showing the HUD, and hiding it in the Facebook completion blocks.

-(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.];
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top