[英]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.