1

我有一个带有 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");
}

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

谢谢你的时间,丹。

4

1 回答 1

2

如前所述,其中一种方法是传递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);

在你上面的方法中。

于 2014-06-25T15:20:16.330 回答