简体   繁体   English

未调用完成处理程序-iOS

[英]Completion handler not called - iOS

I have a simple iOS application with a IBAction. 我有一个带有IBAction的简单iOS应用程序。 However the completion handler code is NOT running and I can't figure out why it's not running. 但是,完成处理程序代码未运行,我无法弄清为什么它未运行。 Here is my code: 这是我的代码:

-(IBAction)didTapSignIn:(id)sender {

void (^handler)(id, id, id) = ^(GTMOAuth2ViewControllerTouch *viewController, GTMOAuth2Authentication *auth, NSError *error) {

    NSLog(@"TEST 2");

    [self dismissViewControllerAnimated:YES completion:^{

        NSLog(@"AC: %@ \nRT: %@", auth.accessToken, auth.refreshToken);

        NSLog(@"AC TOKEN: %@", [GPPSignIn sharedInstance].authentication.accessToken);
        NSLog(@"RF TOKEN: %@", [GPPSignIn sharedInstance].authentication.refreshToken);
        NSLog(@"KEYCHAIN: %@", [GPPSignIn sharedInstance].keychainName);

    }];

    NSLog(@"AC: %@ \nRT: %@", auth.accessToken, auth.refreshToken);

    NSLog(@"AC TOKEN: %@", [GPPSignIn sharedInstance].authentication.accessToken);
    NSLog(@"RF TOKEN: %@", [GPPSignIn sharedInstance].authentication.refreshToken);
    NSLog(@"KEYCHAIN: %@", [GPPSignIn sharedInstance].keychainName);

    /*
    if (error) {
        NSLog(@"%@", error);
        return;
    }

    else {
        BOOL signedIn = [[GPPSignIn sharedInstance] trySilentAuthentication];

        if (!signedIn) {
            NSLog(@"Sign In failed");
        }
    }*/
};

GTMOAuth2ViewControllerTouch *controller = [GTMOAuth2ViewControllerTouch controllerWithScope:@"https://www.googleapis.com/auth/plus.login" clientID:[GPPSignIn sharedInstance].clientID clientSecret:nil keychainItemName:[GPPSignIn sharedInstance].keychainName completionHandler:handler];

NSLog(@"TEST 1");

[self presentViewController:controller animated:YES completion:nil];

NSLog(@"TEST 3");
}

What am I doing wrong? 我究竟做错了什么? It should run but it doesn't. 它应该运行,但不会。

Thanks for your time, Dan. 丹,谢谢您的时间。

As said, one of the ways to to it is to pass handler to [self presentViewController:controller animated:YES completion:handler]; 如前所述,一种实现方法是将handler传递给[self presentViewController:controller animated:YES completion:handler];

But that would only be called when the view controller is dismissed. 但这仅在关闭视图控制器时才调用。

It would be helpful if you could show more specifically GTMOAuth2ViewControllerTouch : 如果您可以更具体地显示GTMOAuth2ViewControllerTouch ,将很有帮助:

Ie, this: 即,这:

[GTMOAuth2ViewControllerTouch controllerWithScope:@"https://www.googleapis.com/auth/plus.login" clientID:[GPPSignIn sharedInstance].clientID clientSecret:nil keychainItemName:[GPPSignIn sharedInstance].keychainName completionHandler:handler];

But given that you're calling dismissViewController in the callback, let assume that you want the completion handler called only when the process is finish and then dismiss the controller: 但是假设您要在回调中调用dismissViewController ,请假设您希望仅在过程完成时才调用完成处理程序,然后关闭控制器:

My guess is that you forgot to do the following when the process is done: 我的猜测是,完成该过程后,您忘记了执行以下操作:

//process.... 
.......
//Done, call the handler
handler(id, id, id);

in your method above. 在上面的方法中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM