2013-08-25 31 views
4

我正在研究一個ios應用程序,最近我有一個新的要求,即爲用戶提供一個選項,使用Gmail登錄。當用戶點擊登錄按鈕,然後我想要打開Gmail登錄屏幕,並在用戶輸入他的憑據後,如果憑證是正確的,那麼不是打開他的郵件,我希望控件導航到我的應用程序主頁。誰能告訴我如何實現這個在ios應用程序中的gmail集成

+0

嘿RU能夠將Gmail集成到你的應用程序。請給我一個GUD鏈接。我試過你的答案,但我得到錯誤錯誤域= com.google.GTMOAuth2代碼= -1001 ..當我去一個不同的代碼之後此鏈接http://technogerms.com/login-with-google-using-oauth-2-0-for-ios-xcode-objective-c/我獲取訪問令牌,但無法使用該訪問令牌檢索用戶詳細信息 – Honey

+0

@arizah,什麼是你的電子郵件ID,我可以發送UA樣本 –

+0

好吧,請將它發送到簡歷中的ID – Honey

回答

2

最後我找到了解決方案。 。 .i認爲這將幫助其他人 按照以下步驟將gmail與您的應用程序集成。

1.爲您的項目添加以下類。

GTMHTTPFetcher.h,GTMHTTPFetcher.m,GTMOAuth2Authentication.h,GTMOAuth2Authentication.m,GTMOAuth2SignIn.h,GTMOAuth2SignIn.m,GTMOAuth2ViewControllerTouch.h,GTMOAuth2ViewControllerTouch.m,GTMOAuth2ViewTouch.xib,SBJSON.h,SBJSON.m

,你會在這裏得到這些類:https://github.com/jonmountjoy/Force.com-iOS-oAuth-2.0-Example

注意:如果您在ARC環境下工作,那麼你必須禁用ARC了以下文件:
GTMHTTPFetcher.m,GTMOAuth2Authentication.m,GTMOAuth2SignIn.m, GTMOAuth2ViewController Touch.m

要在Xcode 4中禁用源文件的ARC,請在Xcode中選擇項目和目標。在目標「構建階段」選項卡下,展開編譯源構建階段,選擇庫源文件,然後按Enter打開編輯字段,然後鍵入-fno-objc-arc作爲這些文件的編譯器標誌。

2.添加以下框架

security.framework , systemConfiguration.framework 

3.註冊應用程式,以前往Google API控制檯...。這裏:https://code.google.com/apis/console

然後轉到ApiAccess部分,爲iOS應用創建客戶端ID。 那麼你將得到clientID,ClientSecret和RedirectUrl

* 4.現在是編碼的時候了。 。 。 。 *
在您的控制器中創建一個登錄按鈕併爲其設置操作。這裏當用戶點擊按鈕SignInGoogleButtonClicked方法被調用。

//import GTMOAuth2Authentication , GTMOAuth2ViewControllerTouch 

#define GoogleClientID @"paster your client id" 
#define GoogleClientSecret @"paste your client secret" 
#define GoogleAuthURL @"https://accounts.google.com/o/oauth2/auth" 
#define GoogleTokenURL @"https://accounts.google.com/o/oauth2/token" 

-(void) SignInGoogleButtonClicked 
{ 

NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL]; 

    NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob"; 

    GTMOAuth2Authentication * auth; 

    auth = [GTMOAuth2Authentication authenticationWithServiceProvider:@"google" 
                  tokenURL:tokenURL 
                  redirectURI:redirectURI 
                  clientID:GoogleClientID 
                 clientSecret:GoogleClientSecret]; 

    auth.scope = @"https://www.googleapis.com/auth/plus.me"; 

    GTMOAuth2ViewControllerTouch * viewcontroller = [[GTMOAuth2ViewControllerTouch alloc] initWithAuthentication:auth 
                           authorizationURL:[NSURL URLWithString:GoogleAuthURL] 
                           keychainItemName:@"GoogleKeychainName" delegate:self 
                           finishedSelector:@selector(viewController:finishedWithAuth:error:)]; 

    [self.navigationController pushViewController:viewcontroller animated:YES]; 

} 



//this method is called when authentication finished 

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)auth error:(NSError *)error 
{ 

    if (error != nil) { 

     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Error Authorizing with Google" 
                 message:[error localizedDescription] 
                 delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
     [alert show]; 
    } 
    else 
    { 

     UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Alert !" 
                 message:@"success" 
                 delegate:nil 
                 cancelButtonTitle:@"OK" 
                 otherButtonTitles:nil]; 
     [alert show]; 

    } 
} 
+1

我一步一步地嘗試這個解決方案,但不工作沒有得到任何錯誤。或者也沒有得到這個Gmail的登錄窗口。 – Jitendra

+0

該代碼爲我工作的人,你可以只檢查一次你的代碼。 –

+0

雅這個代碼工作正常,但我怎麼得到詳細的登錄信息,如facebook圖表api他們提供的所有細節... – Jitendra

0

我發現了。但後來我只是能夠獲取片段,即電子郵件正文&的前幾個字不是整個。我剛剛停止因爲我沒有找到任何其他方式來這樣做。因爲我使用OAuth 2.0。