iOS集成Google 登录

针对iOS端集成Google登录

1、引用pod库

pod 'GoogleSignIn', '~> 6.0.0'

如安装失败
可先更新本地索引 pod repo update
国内pod install最近失败频率较多,多试几次
也可切换下源尝试

    #  source 'https://cdn.cocoapods.org/'
    source 'https://github.com/CocoaPods/Specs.git'
    source 'https://github.com/aliyun/aliyun-specs.git'

2、配置 URL Types

image.png

3、初始化Google单例

signInConfig = [[GIDConfiguration alloc] initWithClientID:@"YOUR_IOS_CLIENT_ID"];

4、处理身份验证重定向网址

- (BOOL)application:(UIApplication *)app
            openURL:(NSURL *)url
            options:(NSDictionary *)options {
  BOOL handled;

  handled = [GIDSignIn.sharedInstance handleURL:url];
  if (handled) {
    return YES;
  }

  // Handle other custom URL types.

  // If not handled by this app, return NO.
  return NO;
}

5、尝试恢复用户的登录状态

- (BOOL)application:(UIApplication *)application
    didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
  [GIDSignIn.sharedInstance restorePreviousSignInWithCallback:^(GIDGoogleUser * _Nullable user,
                                                                NSError * _Nullable error) {
    if (error) {
      // Show the app's signed-out state.
    } else {
      // Show the app's signed-in state.
    }
  }];
  return YES;
}

6、Google Login及Block

[GIDSignIn.sharedInstance signInWithConfiguration:signInConfig
                           presentingViewController:self
                                           callback:^(GIDGoogleUser * _Nullable user,
                                                      NSError * _Nullable error) {
    if (error) {
      return;
    }

    // If sign in succeeded, display the app's main content View.
  }];

7、Google Logout

[GIDSignIn.sharedInstance signOut];

你可能感兴趣的:(iOS集成Google 登录)