本來想說不會很難去串接,但是還是搞了快2~3天,希望這篇文章能幫助到任何卡關的開發者
此文章會從開新的專案開始及怎麼建立ClientID,到Goole登入最後分享文章結束
開發者原文:https://developers.google.com/+/mobile/ios/getting-started
第一步建立ClientID
我們必須到 Google Developers Console 建立一個專案
建立好之後 選擇啟用和管理API
我們必須啟用 Google+ Api的功能
現在我們到憑證頁面
建立 iOS API 金鑰 選擇IOS 輸入BundleID
新增憑證 輸入產品名稱 按儲存 (此部驟大家的畫面上電子郵件地址是有資料的喔)
選擇新增OAuth2.0用戶端ID 選擇IOS 輸入BundleID 以及勾選Google深層連結(deep Link)
此時建立用戶端ID為ClientID 等等串接Google會用到喔
到此部驟完成後,頁面會是這樣喔
第二步 實作功能
將剛剛下載SDK裡面的googleplus.framework, googleOpenSource.framework及googlePlus.bundle放到專案裡面,在專案裡面我們必須參考這些元件
- AddressBook.framework
- AssetsLibrary.framework
- Foundation.framework
- CoreLocation.framework
- CoreMotion.framework
- CoreGraphics.framework
- CoreText.framework
- MediaPlayer.framework
- Security.framework
- SystemConfiguration.framework
- UIKit.framework
及
GooglePlus.frameworkGoogleOpenSource.framework
完成後會是這樣
及
- Google Plus.bundle
增加參數
Other Linker Flags: -ObjC
增加URL Types 輸入BundleID
接下來要開始處理Code摟
在AppDelegate.h裡面增加
搞定!
說明一下
在進入頁面後會去Init GPPSignIn
此時會導去Google的登入頁面,登入後會導回APP
首先呼叫的是APPDelegate的GPPURLHandler
如果在這method當中有其他外部登入的程式碼
可利用Url參數來判斷要執行哪一個
接下來會跑進去ViewController的FinishedWithAuth
在此程式碼裡面,當登入成功後沒有錯誤訊息會將使用者的資料顯示出來
按下按鈕後會彈出分享訊息頁面
在此範例裡面沒有處理到登出及其他相關服務
如果想增加其他功能
可參考這邊
完成!
#import
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation {
return [GPPURLHandler handleURL:url
sourceApplication:sourceApplication
annotation:annotation];
}
在ViewController.h裡面增加在ViewController.m裡面增加#import@interface ViewController : UIViewController{ }- (IBAction) didTapShare: (id)sender;
最後 在storyboard拉一個button綁定didTapShare#importstatic NSString * const kClientID =@"463244965480-75q3f303kecru6v4niq48rotmg11fcoj.apps.googleusercontent.com";- (void)viewDidLoad {[super viewDidLoad];GPPSignIn *signIn = [GPPSignIn sharedInstance];signIn.shouldFetchGooglePlusUser = YES;signIn.clientID = kClientID;signIn.scopes = @[ kGTLAuthScopePlusLogin ];signIn.delegate = self;[signIn authenticate];}- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error{if (error) {// Do some error handling here.} else {GTLPlusPerson *person = [GPPSignIn sharedInstance].googlePlusUser;if (person != nil){if ([[GPPSignIn sharedInstance].userEmail length] > 0){GTLPlusPersonName *googleUserName = person.name ;NSLog(@"%@ %@ %@ %@",googleUserName.givenName,googleUserName.familyName,person.gender,person.displayName );NSMutableDictionary *dicData = [[NSMutableDictionary alloc] init];[dicData setObject:[GPPSignIn sharedInstance].userEmail forKey:@"email"];[dicData setObject:[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleVersion"] forKey:@"app_version"];[dicData setValue:googleUserName.givenName forKey:@"firstname"];[dicData setValue:googleUserName.familyName forKey:@"lastname"];NSData *avatarData = nil;NSString *imageURLString = person.image.url;imageURLString = [imageURLString stringByReplacingOccurrencesOfString:@"sz=50" withString:@"sz=300"];NSURL *imageURL = [NSURL URLWithString:imageURLString];avatarData = [NSData dataWithContentsOfURL:imageURL];// [avatarData writeToFile:[self getSocialProfilePicPath] atomically:NO];}else{// [self alertshow:@"Google" :error.localizedDescription];}}else{// [self alertshow:@"Google" :error.localizedDescription];}NSLog(@"Google Plus Logged %@",person);NSLog(@"User Email Address = %@",[GPPSignIn sharedInstance].userEmail);}}- (IBAction) didTapShare: (id)sender {idshareBuilder = [[GPPShare sharedInstance] nativeShareDialog]; [shareBuilder setURLToShare:[NSURL URLWithString:@"https://www.example.com/restaurant/sf/1234567/"]];[shareBuilder setPrefillText:@"I made reservations!"];[shareBuilder setContentDeepLinkID:@"rest=1234567"];[shareBuilder setCallToActionButtonWithLabel:@"RSVP"URL:[NSURL URLWithString:@"https://www.example.com/reservation/4815162342/"]deepLinkID:@"rsvp=4815162342"];[shareBuilder open];}
搞定!
說明一下
在進入頁面後會去Init GPPSignIn
此時會導去Google的登入頁面,登入後會導回APP
首先呼叫的是APPDelegate的GPPURLHandler
如果在這method當中有其他外部登入的程式碼
可利用Url參數來判斷要執行哪一個
接下來會跑進去ViewController的FinishedWithAuth
在此程式碼裡面,當登入成功後沒有錯誤訊息會將使用者的資料顯示出來
按下按鈕後會彈出分享訊息頁面
在此範例裡面沒有處理到登出及其他相關服務
如果想增加其他功能
可參考這邊
完成!












