如何在 iPhone 上使用 Google+ 注销
How to sign out with Google+ on iPhone
我正在编写一个用户可以使用 Google+ 登录的应用程序。我按照GOOGLE开发者控制台成功登录并通过Access_Token获取了用户资料信息。我想通过web视图登录,但是登录后如何退出?
我的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)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)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:@"POST"];
[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;
}
您不再需要自己执行此操作。从 2.0.0 开始,Google Sign-in with the Identity SDK 将允许您使用网络视图。
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)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
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)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
我正在编写一个用户可以使用 Google+ 登录的应用程序。我按照GOOGLE开发者控制台成功登录并通过Access_Token获取了用户资料信息。我想通过web视图登录,但是登录后如何退出?
我的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)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)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:@"POST"];
[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;
}
您不再需要自己执行此操作。从 2.0.0 开始,Google Sign-in with the Identity SDK 将允许您使用网络视图。
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)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
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)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