iOS集成Google+登录
1. 创建项目
打开Google后台:https://console.developers.google.com/apis,点击左上角My Project
,在弹出框点击```新建项目
文章图片
创建项目
接下来申请配置项目参数,在https://developers.google.com/identity/sign-in/ios/start-integrating
中点击Create an OAuth clientID
按钮,选择刚才创建的项目,选择平台(iOS),输入项目BundleID,成功之后会提示下载一个plist文件,里面包含了配置参数
文章图片
plist文件
2.Xcode工程配置
在xcode工程的info文件中配置url scheme
文章图片
scheme
URL Schemes参数填plist文件中的REVERSED_CLIENT_ID
3. OC代码
根据需要在合适的地方声明协议:
@import GoogleSignIn;
@interface AppDelegate : UIResponder
代码配置参数和代理:
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {[GIDSignIn sharedInstance].clientID = @"YOUR_CLIENT_ID";
[GIDSignIn sharedInstance].delegate = self;
return YES;
}
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary *)options {
return [[GIDSignIn sharedInstance] handleURL:url];
}
在登录回调中处理登录流程:
- (void)signIn:(GIDSignIn *)signIn
didSignInForUser:(GIDGoogleUser *)user
withError:(NSError *)error {
if (error != nil) {
if (error.code == kGIDSignInErrorCodeHasNoAuthInKeychain) {
NSLog(@"The user has not signed in before or they have since signed out.");
} else {
NSLog(@"%@", error.localizedDescription);
}
return;
}
// 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 *fullName = user.profile.name;
NSString *givenName = user.profile.givenName;
NSString *familyName = user.profile.familyName;
NSString *email = user.profile.email;
// ...
}- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
withError:(NSError *)error {
// Perform any operations when the user disconnects from app here.
// ...
}
注:
如果想要SDK刷新accessToken,可以调用getAccessTokenWithHandler:
方法,如果 想要刷新accessToken,调用refreshAccessTokenWithHandler:
方法。调用登录:
[[XGOVGoogleSign sharedModel] setComplete:^(NSDictionary *_Nonnull response) {
NSLog(@"登录验证...");
}];
[[XGOVGoogleSign sharedModel] signIn];
【iOS集成Google+登录】登出:
- (IBAction)didTapSignOut:(id)sender {
[[GIDSignIn sharedInstance] signOut];
}
推荐阅读
- Activiti(一)SpringBoot2集成Activiti6
- 2020-04-07vue中Axios的封装和API接口的管理
- iOS中的Block
- 私有化轻量级持续集成部署方案--03-部署web服务(下)
- 记录iOS生成分享图片的一些问题,根据UIView生成固定尺寸的分享图片
- 2019-08-29|2019-08-29 iOS13适配那点事
- Hacking|Hacking with iOS: SwiftUI Edition - SnowSeeker 项目(一)
- iOS面试题--基础
- 接口|axios接口报错-参数类型错误解决
- iOS|iOS 笔记之_时间戳 + DES 加密