iOS中的Gmail集成示例 [英] Example for Gmail Integration in ios

查看:40
本文介绍了iOS中的Gmail集成示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个应用程序,其中有一个名为登录"的按钮,当用户单击该按钮时,应显示gmail登录页面,一旦用户提供了其凭据,而不是登录到邮件主页,应该调用应用程序的注册页面,其中填写了从用户的gmail个人资料中获取的详细信息.(详细信息,例如用户的名字和姓氏,电子邮件等).搜索为我提供了以下网站" https://code.google.com/p/gtm-oauth/wiki/GTMOAuthIntroduction "但是我需要一个示例来了解gmail集成的工作原理...预先感谢

I am doing an app in which , i have a button called Login, when the user click on the button the gmail login page should be displayed, once the user provides his credentials, instead of logging in to the mail home page it should call the registration page of the app with the details filled that are fetched from the gmail profile of the user.. (Details like user first and last name, email so on).... Search provided me below site "https://code.google.com/p/gtm-oauth/wiki/GTMOAuthIntroduction" But i need an example to know how exactly the gmail integration works... Thanks in advance

推荐答案

最后,我找到了解决方案...我认为这会有所帮助

Finally i found the solution . . . .i think this will help

按照以下步骤将gmail与您的应用程序集成.

Follow the below steps to integrate gmail with your application .

1.将以下类添加到您的项目中.

1.Add following classes to you project .

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-例子

注意:如果您在ARC环境下工作,则必须禁用以下文件的ARC:

Note : if you are working under ARC Environment then you have to disable the ARC for following files :

GTMHTTPFetcher.m , GTMOAuth2Authentication.m , GTMOAuth2SignIn.m, GTMOAuth2ViewControllerTouch.m

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

To disable ARC for source files in Xcode 4, select the project and the target in Xcode. Under the target "Build Phases" tab, expand the Compile Sources build phase, select the library source files, then press Enter to open an edit field, and type -fno-objc-arc as the compiler flag for those files.

  1. 添加以下框架

  1. add the following frameworks

security.framework,systemConfiguration.framework

security.framework , systemConfiguration.framework

将您的应用注册到Google api控制台….此处: https://code.google.com/apis/console

Register your app to google api console …. here : https://code.google.com/apis/console

然后转到ApiAccess部分,为iOS应用创建客户端ID.那么您将获得clientID,ClientSecret和RedirectUrl

现在是时候进行编码了...在您的控制器中创建一个SignIn按钮,并为此设置操作.在这里,当用户单击按钮SignInGoogleButtonClicked方法时,就会调用

Now it's time for coding . . . .create a signIn button in your controller and set the action for that . Here when the user click the button SignInGoogleButtonClicked method gets called

代码

 #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]

    }
}

这篇关于iOS中的Gmail集成示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆