進入 https://console.developers.google.com/apis 選擇google+ api,
然后選擇管理,
點擊紅色區(qū)域

然后新建一個項目

輸入你的項目名,然后創(chuàng)建

接下來配置,參照這個鏈接
https://developers.google.com/identity/sign-in/ios/start-integrating
(還有視頻講解)
點擊創(chuàng)建憑據(jù)(CREATE AN OAUTH CLIENT ID),選擇你剛才創(chuàng)建的項目,選擇平臺(IOS),輸入BundleID

成功之后會讓你下載Json文件,
其中 CLIENT_ID 是在AppDelegate文件中GIDSignIn.sharedInstance().clientID (swift)的值
REVERSED_CLIENT_ID在下面這里設(shè)置

接下來參照這個
https://developers.google.com/identity/sign-in/ios/sign-in
AppDelegate添加GIDSignInDelegate協(xié)議
然后
//設(shè)置好代理和clientID
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GIDSignIn sharedInstance].clientID = @"YOUR_CLIENT_ID";
[GIDSignIn sharedInstance].delegate = self;
return YES;
}
//第三方登陸后回調(diào) 這邊稍微注意一下自己要適配的IOS系統(tǒng)版本
- (BOOL)application:(UIApplication *)app
openURL:(NSURL *)url
options:(NSDictionary<NSString *, id> *)options {
return [[GIDSignIn sharedInstance] handleURL:url
sourceApplication:options[UIApplicationOpenURLOptionsSourceApplicationKey]
annotation:options[UIApplicationOpenURLOptionsAnnotationKey]];
}
關(guān)于didSignInForUser這個方法,我OC沒有在appDelegate寫這個函數(shù),并沒有什么影響,不過swift里沒寫報錯了(不過我swift寫得比較晚,也許sdk更新了吧)
Google+有一個專門的GIDSignInButton,不過我是自己寫的Button,在button的點擊事件里調(diào)用
[[GIDSignIn sharedInstance] signIn];就行,
當然在你的這個button所在的控制器里,要添加代理
GIDSignIn*signIn = [GIDSignIn sharedInstance];
signIn.shouldFetchBasicProfile=YES;
signIn.delegate=self;
signIn.uiDelegate=self;
登錄成功后的回調(diào)
- (void)signIn:(GIDSignIn*)signIn didSignInForUser:(GIDGoogleUser*)user withError:(NSError*)error
{
NSLog(@"user %@",user);
NSLog(@"error %@",error);
}
之前用OC寫了一版,今年又被要求用Swift寫一版,基本大同小異。憑借著一些印象,記錄一下這個配置。如果有不對的地方,麻煩幫忙指出。