タイトル:ios-google接入
カテゴリ:Ios
タグ:[ios、google]
日付:2021-02-15 19:58:43
コメント:false
mathjax:true
toc:true
ios-googleアクセス
前日譚
- 公式-https://developers.google.cn/identity/sign-in/ios/start-integrating
グーグルアクセス
-
GoogleAPIバックグラウンドでiOSタイプのクライアントIDと逆クライアントIDを取得します。
iOSタイプのクライアントIDを作成します
[外部リンク画像の転送に失敗しました。ソースサイトにヒル防止リンクメカニズムがある可能性があります。画像を保存して直接アップロードすることをお勧めします(img-Hz8JMeFO-1613393112071)(http://yxbl.itengshe.com/20210215204243- 1.webp)]
-
CocoaPodsはいくつかのコアライブラリを導入します
pod 'GoogleSignIn', '5.0.2'
-
Info.plistファイル
<dict>...</dict>
ここで使用される構成に追加逆クライアントIDです。(以降com.googleusercontent.apps、いないクライアントID)<key>CFBundleURLTypes</key> <array> <dict> <key>CFBundleTypeRole</key> <string>Editor</string> <key>CFBundleURLSchemes</key> <array> <string>com.googleusercontent.apps.1086111-l3k4nlbbb</string> </array> </dict> </array>
または、info.plistのGUIパネルで構成を追加します
-
コード
-
アプリの起動後にSDKを初期化します
// AppDelegate.m @implementation AppDelegate - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [GIDSignIn sharedInstance].clientID = @"1086444997895-l3k4nl4oq9allsq7rga9adiaeth2g8n9.apps.googleusercontent.com"; // client id [GIDSignIn sharedInstance].delegate = GGHelper.shareInstance; return YES; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url options:(nonnull NSDictionary<UIApplicationOpenURLOptionsKey, id> *)options{ [[GIDSignIn sharedInstance] handleURL:url]; return YES; }
-
ログイン、ログアウト
// GGHelper.h #import <GoogleSignIn/GoogleSignIn.h> @interface GGHelper : NSObject <GIDSignInDelegate> // GIDSignInDelegate 登录结果回调 +(instancetype) shareInstance; +(void)login:(CodeIdFn)cb; +(void)logout:(CodeIdFn)cb; @end // GGHelper.m #import "GGHelper.h" #import "Tool.h" @implementation GGHelper static GGHelper *_sharedIns = nil; +(instancetype) shareInstance { static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _sharedIns = [[self alloc] init] ; }) ; return _sharedIns ; } +(void)login:(CodeIdFn)cb { [GIDSignIn sharedInstance].presentingViewController = [Tool getRootViewCtrl]; [[GIDSignIn sharedInstance] signIn]; // 调用这个接口是一定要给 presentingViewController 赋值一个 UIViewController, 不然会闪退, 卧槽 // [[GIDSignIn sharedInstance] restorePreviousSignIn]; // 自动登录 } +(void)logout:(CodeIdFn)cb { [[GIDSignIn sharedInstance] signOut]; } // ------------------------------- 实现 GIDSignInDelegate 的接口 - (void)signIn:(GIDSignIn *)signIn didSignInForUser:(GIDGoogleUser *)user withError:(NSError *)error { if (error != nil) { if (error.code == kGIDSignInErrorCodeHasNoAuthInKeychain) { // restorePreviousSignIn 自动登录失败会回调到这里. NSLog(@"--- didSignInForUser, The user has not signed in before or they have since signed out."); } else { NSLog(@"--- didSignInForUser, err: %@", error); } 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; NSLog(@"--- login success, user: %@, fullName: %@", userId, fullName); // ... } - (void)signIn:(GIDSignIn *)signIn didDisconnectWithUser:(GIDGoogleUser *)user withError:(NSError *)error { NSLog(@"--- didDisconnectWithUser, err: %@", error); } @end
-
サーバーチェックフィールド
-
トークンリクエストが成功すると、そのようなフィールドが返されます
{ "azp": "10864aaa-l3kbbb.apps.googleusercontent.com", "aud": "10864aaa-l3kbbb.apps.googleusercontent.com", "given_name": "Wilker", }
azpとaudは、google apiバックグラウンドによって取得されたiosタイプのクライアントID値であるため、クライアントIDは、ログインが成功したかどうかを判断するために使用されます。
これはAndroidと同じではありません。Androidから返される2つの値は、どちらもWebクライアントIDです。
ピットを踏む
ログインインターフェイスを呼び出してクラッシュする
[[GIDSignIn sharedInstance] signIn]
与えられたフラッシュバックを呼び出します:reason: 'presentingViewController must be set.'
UIViewControllerをpresentingViewControllerに割り当てる必要があります