美文网首页
iOS集成Google 登录

iOS集成Google 登录

作者: coco_CC | 来源:发表于2022-07-28 14:10 被阅读0次

    针对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<UIApplicationOpenURLOptionsKey,id> *)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 登录

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