google第三方登录
转载声明:
本文为摘录自“简书”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2020-08-26 15:18:25
选择创建应用类型地址:https://developers.google.com/identity/
查看应用管理地址:https://console.developers.google.com/cloud-resource-manager
下载sdk地址:https://developers.google.com/identity/sign-in/ios/sdk/
获得配置文件
App name:填写app名称、iOS Bundle ID:填写应用bundle id(类似com.google.asia5b.first)
保存配置文件
添加两个schemes:一个reversed_client_id、一个bundle_id
白名单:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>com.google.gppconsent.2.4.1</string>
<string>com.google.gppconsent.2.4.0</string>
<string>com.google.gppconsent.2.3.0</string>
<string>com.google.gppconsent.2.2.0</string>
<string>com.google.gppconsent</string>
<string>hasgplus4</string>
<string>googlechrome-x-callback</string>
<string>googlechrome</string>
</array>
在appdelegate.m里面添加代理:
<GIDSignInDelegate>
在
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;
里面添加:
[GIDSignIn sharedInstance].delegate = self;
[GIDSignIn sharedInstance].clientID = Google_CLIENT_ID;
代理方法:
- (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 *fullName = user.profile.name;
NSString *givenName = user.profile.givenName;
NSString *familyName = user.profile.familyName;
NSString *email = user.profile.email;
NSLog(@"userID = %@",userId);
NSLog(@"idToken = %@",idToken);
NSLog(@"fullName = %@",fullName);
NSLog(@"givenName = %@",givenName);
NSLog(@"familyName = %@",familyName);
NSLog(@"email = %@",email);
}
-(void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error{
//执行任何操作,当用户断开与这里的应用程序。// …
}
添加系统回调方法:
// 支持所有iOS系统
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation{
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url sourceApplication:sourceApplication annotation:annotation];
if ([url.absoluteString rangeOfString:Google_REVERSED_CLIENT_ID].location != NSNotFound) {
//Google的回调
return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:sourceApplication annotation:annotation];
}
return result;
}
//仅支持iOS9以上系统,iOS8及以下系统不会回调
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options{
//6.3的新的API调用,是为了兼容国外平台(例如:新版facebookSDK,VK等)的调用[如果用6.2的api调用会没有回调],对国内平台没有影响
BOOL result = [[UMSocialManager defaultManager] handleOpenURL:url options:options];
if ([url.absoluteString rangeOfString:Google_REVERSED_CLIENT_ID].location != NSNotFound) {
//Google的回调
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
return result;
}
在需要登录的viewController的添加代理:
<GIDSignInUIDelegate,GIDSignInDelegate>
viewdidload里添加:
[GIDSignIn sharedInstance].uiDelegate = self;
在自定义登录按钮中添加:
[[GIDSignIn sharedInstance] signIn];
[GIDSignIn sharedInstance].delegate = self;
[GIDSignIn sharedInstance].clientID = Google_CLIENT_ID;
实现代理方法:
#pragma mark - GIDSignInDelegate
- (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error{
if (error) {
NSLog(@"error = %@",error);
}else{
NSString *token = user.authentication.idToken;
NSString *openId = user.userID;
NSString *nickname = user.profile.name;
NSDictionary *resultDic = @{@"openid”:openId,
@"nickname”:nickname,
@"account_type":@“google”,
@"access_token”:token,
@"third_appid":Google_APP_ID};
//获得返回数据resultDic实现客户端登录
[self requestWithUSID:openId Picurl:@"" Name:nickname Type:@"2”];
}
}
- (void)presentSignInViewController:(UIViewController *)viewController {
[self presentViewController:viewController animated:YES completion:nil];
}
在退出按钮里添加:
[[GIDSignIn sharedInstance] signOut];