OGeek|极客世界-中国程序员成长平台

标题: ios - 如何在 iPhone 上使用 Google+ 退出 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 09:29
标题: ios - 如何在 iPhone 上使用 Google+ 退出

我正在编写一个用户可以使用 Google+ 登录的应用程序。我跟着GOOGLE开发者控制台成功登录,通过Access_Token获取用户文件信息。我想通过网页 View 登录,但是登录后如何注销?

我的 Webview 方法

-(void)addWebView
{

    NSString *url = [NSString stringWithFormat"https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=%@&redirect_uri=%@&scope=%@&data-requestvisibleactions=%@",client_id,callbakc,scope,visibleactions];

    self.webview = [[UIWebView alloc]init];
    self.webview.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    self.webview.delegate = self;
    [self.view addSubview:self.webview];
    [self.webview  loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:url]]];


}
- (BOOL)webViewUIWebView*)webView shouldStartLoadWithRequestNSURLRequest*)request navigationTypeUIWebViewNavigationType)navigationType {
    //    [indicator startAnimating];
    if ([[[request URL] host] isEqualToString"localhost"]) {

        // Extract oauth_verifier from URL query
        NSString* verifier = nil;
        NSArray* urlParams = [[[request URL] query] componentsSeparatedByString"&"];
        for (NSString* param in urlParams) {
            NSArray* keyValue = [param componentsSeparatedByString"="];
            NSString* key = [keyValue objectAtIndex:0];
            if ([key isEqualToString"code"]) {
                verifier = [keyValue objectAtIndex:1];
                NSLog(@"verifier %@",verifier);
                break;
            }
        }

        if (verifier) {
            NSString *data = [NSString stringWithFormat"code=%@&client_id=%@&client_secret=%@&redirect_uri=%@&grant_type=authorization_code", verifier,client_id,secret,callbakc];
            NSString *url = [NSString stringWithFormat"https://accounts.google.com/o/oauth2/token"];
            NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
            [request setHTTPMethod"OST"];
            [request setHTTPBody:[data dataUsingEncoding:NSUTF8StringEncoding]];
            NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];
            receivedData = [[NSMutableData alloc] init];

        } else {
            // ERROR!
        }

        [webView removeFromSuperview];

        return NO;
    }
    return YES;
}



Best Answer-推荐答案


您不再需要自己执行此操作。从 2.0.0 开始,Google Sign-in with the Identity SDK将允许您使用 webview。

- (BOOL)applicationUIApplication *)application
didFinishLaunchingWithOptionsNSDictionary *)launchOptions {
  NSError* configureError;
  [[GGLContext sharedInstance] configureWithError: &configureError];
  NSAssert(!configureError, @"Error configuring Google services: %@", configureError);
  [GIDSignIn sharedInstance].allowsSignInWithWebView = YES;
  [GIDSignIn sharedInstance].allowsSignInWithBrowser = NO;
  [GIDSignIn sharedInstance].delegate = self;
  // ...
}

用户登录后,您将在 didSignInForUser 中收到所有相关详细信息:

- (void)signIn    GIDSignIn *)signIn
  didSignInForUserGIDGoogleUser *)user
         withErrorNSError *)error {
  // Perform any operations on signed in user here.
  NSString *userId = user.userID;                  // For client-side use only!
  NSString *idToken = user.authentication.idToken; // Safe to send to the server
  NSString *name = user.profile.name;
  NSString *email = user.profile.email;
}

稍后,当您想将用户注销时,只需在 sharedInstance 单例上调用 SignOut 方法即可:

[GIDSignIn sharedInstance].signOut();

您应该试用 Google 登录示例以查看有关如何使用 SDK 的完整示例:

pod try Google

关于ios - 如何在 iPhone 上使用 Google+ 退出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31801446/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4