finishedWithAuth:error: method calling after Google+ authentication in UIWebView

finishedWithAuth:error: method calling after Google+ authentication in UIWebView

我已经成功地将 Google+ 集成到我的 iOS 应用程序中。但是随着苹果商店的最新更新,不允许该应用程序打开浏览器发起Google 身份验证。因此,我需要在我的应用程序中创建一个 Web 界面。

所以我按照下面的方式做了,

我限制打开浏览器应用进行身份验证的地方URL

   - (BOOL)openURL:(NSURL *)url {
    NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);

    if ([[url absoluteString] hasPrefix:@"googlechrome-x-callback:"]) {

        return NO;

    } else if ([[url absoluteString] hasPrefix:@"https://accounts.google.com/o/oauth2/auth"]) {

        NSDictionary *userInfo = [NSDictionary dictionaryWithObject:url forKey:@"authUrl"];

        [[NSNotificationCenter defaultCenter] postNotificationName:@"ApplicationOpenGoogleAuthNotification" object:nil userInfo:userInfo];
        return NO;
    }

    return [super openURL:url];
}

在我的 ViewController 用户点击 Google+ 登录按钮的地方,我放置了这个观察者

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(callInternalWeb:) name:@"ApplicationOpenGoogleAuthNotification" object:nil];

这将加载一个不同的 UIViewController,其中有一个 UIWebView

   - (void)callInternalWeb:(NSNotification *)notification {
    NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);

    NSDictionary *userInfo = notification.userInfo;

    [self performSegueWithIdentifier:@"googleAuthSegue" sender:userInfo];
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);

    GoogleAuthViewController *googleAuthViewController = [segue destinationViewController];
    [googleAuthViewController navigateUrl:sender];
}

然后在 GoogleAuthViewController 中,我试图用我使用 NSNotification

传递的 URL 加载 UIWebView
NSString *formatStr = [NSString stringWithFormat:@"%@",googleAuthUrlStr];

NSURL *url = [NSURL URLWithString:formatStr];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

_webView.delegate = self;

[_webView loadRequest:request];

Google 登录完成后,我可以重定向回 UIViewController,如下所示

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);

    if ([[[request URL] absoluteString] hasPrefix:@"my.bunble.googleplusintegration:/oauth2callback"]) {

        [self dismissViewControllerAnimated:YES completion:nil];

        return NO;
    }
    return YES;
}

但我无法获取 Google 用户详细信息,因为未调用以下方法

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error {}

早些时候我能够在上面提到的方法中获得如下用户详细信息

if ([GPPSignIn sharedInstance].authentication) {

    GTLPlusPerson *googleUser = [signIn googlePlusUser];

    NSString *firstName = [[googleUser name] givenName];
    NSString *lastName = [[googleUser name] familyName];
    NSString *dob = [googleUser birthday];
    NSString *gender = [googleUser gender];
    NSString *gUserId = [googleUser identifier];
    NSString *gUserEmail = signIn.authentication.userEmail;

    GTLPlusPersonImage *image = [googleUser image];
}

如何获取这些详细信息?

以上代码完全正确。遗漏的部分是

[GPPURLHandler handleURL:[request URL] sourceApplication:@"com.apple.mobilesafari" annotation:nil];

所以完整的代码如下所示

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    NSLog(@"%s - %d", __PRETTY_FUNCTION__, __LINE__);

    if ([[[request URL] absoluteString] hasPrefix:@"my.bunble.googleplusintegration:/oauth2callback"]) {
    [GPPURLHandler handleURL:[request URL] sourceApplication:@"com.apple.mobilesafari" annotation:nil];

        [self dismissViewControllerAnimated:YES completion:nil];

        return NO;
    }
    return YES;
}