iOS集成Google+登录

1. 创建项目
打开Google后台:https://console.developers.google.com/apis,点击左上角My Project,在弹出框点击```新建项目

iOS集成Google+登录_第1张图片
创建项目

接下来申请配置项目参数,在https://developers.google.com/identity/sign-in/ios/start-integrating
中点击Create an OAuth clientID按钮,选择刚才创建的项目,选择平台(iOS),输入项目BundleID,成功之后会提示下载一个plist文件,里面包含了配置参数

iOS集成Google+登录_第2张图片
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];

登出:

- (IBAction)didTapSignOut:(id)sender {
  [[GIDSignIn sharedInstance] signOut];
}

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