美文网首页
Google Sign-In for iOS

Google Sign-In for iOS

作者: 拂溪 | 来源:发表于2019-11-05 09:30 被阅读0次

一、控制台创建项目

1、新建项目
image.png
image.png
2、创建凭证
image.png

在步骤4中填写你项目的bundle ID


image.png
成功之后会让你下载json文件,打开后如下图 image.png
其中 CLIENT_ID 是在AppDelegate文件中GIDSignIn.sharedInstance().clientID 的值

二、将谷歌登录集成到iOS应用程序

在开始将iOS应用程序与谷歌登录组件集成之前,必须下载依赖项并配置Xcode项目。

1、设置CocoaPods依赖项

在终端打开你的Podfile文件,添加pod 'GoogleSignIn'

pod 'GoogleSignIn'

保存文件并运行

pod install

打开应用程序的.xcworkspace文件。

2、Add a URL scheme

(1)打开项目配置:双击左侧树视图中的项目名称。从“目标”部分选择应用程序,然后选择“信息”选项卡,并展开“URL类型”部分。
(2)单击+按钮,并将反向的客户端ID添加为URL方案。

颠倒的客户机ID是您的客户机ID,并且颠倒了点分隔字段的顺序。
例如:com.googleusercontent.apps.1234567890-abcdefg

当完成配置后,你的配置应该看起来类似于下面的东西(但是你的应用程序特定的值):


image.png
3、Enable sign-in(启用登录)

要启用登录,必须配置GIDSignIn共享实例。你可以在你的应用程序的很多地方这样做。通常,配置这个实例最简单的地方是在你的应用程序委托的应用程序中:didFinishLaunchingWithOptions: method。
(1)在应用程序委托的.h文件中,声明该类实现了GIDSignInDelegate协议。

@import GoogleSignIn;
@interface AppDelegate : UIResponder <UIApplicationDelegate, GIDSignInDelegate>

(2)在应用程序委托的应用程序中:didFinishLaunchingWithOptions:方法中,配置GIDSignIn共享实例并设置登录委托。

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

  [GIDSignIn sharedInstance].clientID = @"YOUR_CLIENT_ID";
  [GIDSignIn sharedInstance].delegate = self;

  return YES;
}

(3)实现应用程序委托的应用程序:openURL:options:方法。该方法应该调用GIDSignIn实例的handleURL方法,该方法将正确处理应用程序在身份验证过程结束时接收到的URL。

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary<NSString *, id> *)options {
  return [[GIDSignIn sharedInstance] handleURL:url sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
             annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];

}

如果您的应用程序要在ios8或更老版本上运行,还需要实现不推荐的应用程序:openURL:sourceApplication:annotation: method。

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation {

  return [[GIDSignIn sharedInstance] handleURL:url
                             sourceApplication:sourceApplication
                                    annotation:annotation];
}

(4)在app委托中,通过定义以下方法实现GIDSignInDelegate协议来处理登录过程:

AppDelegate.m

- (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;
  // ...
}

AppDelegate.m

- (void)signIn:(GIDSignIn *)signIn
didDisconnectWithUser:(GIDGoogleUser *)user
     withError:(NSError *)error {
  // Perform any operations when the user disconnects from app here.
  // ...
}

注意:登录SDK会自动获取访问令牌,但是只有在您调用signIn或静默地登录时才会刷新访问令牌。要显式地刷新访问令牌,请调用refreshTokensWithHandler:方法。如果需要访问令牌并希望SDK自动处理刷新它,可以使用getTokensWithHandler:方法。

important:如果需要将当前登录的用户传递到后端服务器,请将用户的ID令牌发送到后端服务器并在服务器上验证该令牌。

(5)设置登录按钮
Google+有一个专门的GIDSignInButton,不过我是自己写的Button,在button的点击事件里调用
[[GIDSignIn sharedInstance] signIn];就行,


image.png
- (void)loginAction {
    NSLog(@"click login");
    [[GIDSignIn sharedInstance] signIn];
}

- (void)logoutAction {
     NSLog(@"click logout");
     [[GIDSignIn sharedInstance] signOut];
}

登录成功后的回调

- (void)signIn:(GIDSignIn*)signIn didSignInForUser:(GIDGoogleUser*)user withError:(NSError*)error {
    NSLog(@"user %@",user);
    NSLog(@"error %@",error);
}

运行后,点击“login”


image.png

相关文章

网友评论

      本文标题:Google Sign-In for iOS

      本文链接:https://www.haomeiwen.com/subject/hazrbctx.html