[英]Completion handler not called - iOS
我有一个带有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");
}
我究竟做错了什么? 它应该运行,但不会。
丹,谢谢您的时间。
如前所述,一种实现方法是将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);
在上面的方法中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.