Gmail集成在iOS中的示例

我正在做一个应用程序,其中,我有一个名为loginbutton,当用户点击button应显示Gmaillogin页面,一旦用户提供了他的凭据,而不是login到邮件主页应该调用从用户的gmailconfiguration文件中获取详细信息的应用程序的注册页面。(详细信息如用户姓名,电子邮件等)….search提供我下面的网站“ https://代码。 google.com/p/gtm-oauth/wiki/GTMOAuthIntroduction “但是我需要一个例子来了解gmail集成是如何工作的……在此先感谢

最后我find了解决办法。 。 。 我想这会有所帮助

按照以下步骤将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作为这些文件的编译器标志。

  1. 添加以下框架

    security.framework,systemConfiguration.framework

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

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

  3. 现在是编码的时候了。 。 。 。在你的控制器中创build一个loginbutton并为其设置动作。 这里当用户点击buttonSignInGoogleButtonClicked方法被调用

  #define GoogleAuthURL @"https://accounts.google.com/o/oauth2/auth" #define GoogleTokenURL @"https://accounts.google.com/o/oauth2/token" #define GoogleClientID @"paste your client id" #define GoogleClientSecret @"paste your client secret" -(void) SignInGoogleButtonClicked{ NSURL * tokenURL = [NSURL URLWithString:GoogleTokenURL]; NSString * redirectURI = @"urn:ietf:wg:oauth:2.0:oob"; GTMOAuth2Authentication * 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] } }