gmail集成在ios应用程序中

我正在一个IOS应用程序,最近我有一个新的要求,即为用户提供一个选项,使用Gmaillogin。 当用户点击loginbutton,然后我想打开Gmaillogin屏幕,并在用户input他的凭据后,如果凭据是正确的,而不是打开他的邮件,我希望控制导航到我的应用程序主页。 有谁可以告诉我如何做到这一点

最后我find了解决办法。 。 .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,GTMOAuth2ViewControllerTouch.m

要在Xcode 4中禁用源文件的ARC,请在Xcode中select项目和目标。 在目标“构build阶段”选项卡下,展开“编译源”构build阶段,select库源文件,然后按Enter打开编辑字段,然后键入-fno-objc-arc作为这些文件的编译器标志。

2.添加以下框架

security.framework , systemConfiguration.framework 

3.注册你的应用程序到谷歌的API控制台…。 这里: https : //code.google.com/apis/console

然后转到ApiAccess部分,为iOS应用程序创build客户端ID。 那么你将得到clientID,ClientSecret和RedirectUrl

* 4.现在是编码的时候了。 *
在您的控制器中创build一个loginbutton,并为此设置操作。 这里当用户点击buttonSignInGoogleButtonClicked方法被调用。

 //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]; } } 

我find了。 但后来我只是能够获取片段,即电子邮件正文的最初几个字,而不是整体。 我刚刚停止因为我没有find任何其他的方式来这样做。 因为我使用的是OAuth 2.0。