【问题标题】:Completion handler not called - iOS未调用完成处理程序 - iOS
【发布时间】:2014-08-16 04:56:15
【问题描述】:

我有一个带有 IBAction 的简单 iOS 应用程序。但是,完成处理程序代码没有运行,我无法弄清楚它为什么没有运行。这是我的代码:

-(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");
}

我做错了什么?它应该运行,但它没有。

感谢您抽出宝贵时间,丹。

【问题讨论】:

  • GTMOauth2ViewControllerTouch 中的completionHandler 做了什么?如果你在展示你的控制器后想要完成处理程序,你应该调用 [self presentViewController:controller animated:YES completion:handler];

标签: ios objective-c cocoa-touch methods completionhandler


【解决方案1】:

如前所述,其中一种方法是将handler 传递给[self presentViewController:controller animated:YES completion:handler];

但这只会在视图控制器被关闭时调用。

如果您能更具体地显示GTMOAuth2ViewControllerTouch,将会很有帮助:

也就是说,这个:

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

但鉴于您在回调中调用 dismissViewController,假设您希望仅在进程完成时调用完成处理程序,然后关闭控制器:

我的猜测是您在完成该过程时忘记执行以下操作:

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

在你上面的方法中。

【讨论】:

  • 我不完全确定你的意思,但还是谢谢你。
猜你喜欢
  • 2013-03-24
  • 2021-03-18
  • 1970-01-01
  • 1970-01-01
  • 2019-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多