gos集成在ios应用程序中 [英] gmail integration in ios application

查看:186
本文介绍了gos集成在ios应用程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究一个ios应用程序,最近我对它有了一个新的要求,即为用户提供一个使用gmail登录的选项。当用户点击登录按钮,然后我想要打开Gmail登录屏幕,并在用户输入他的凭据后,如果凭证是正确的,那么不是打开他的邮件,我希望控件导航到我的应用程序主页。任何人都可以告诉我如何实现这个解决方案。最后我找到了解决方案。 。 .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中选择项目和目标。在目标构建阶段选项卡下,展开编译源构建阶段,选择库源文件,然后按Enter打开编辑字段,并键入-fno-objc-arc作为这些文件的编译器标志。



2。添加以下框架

  security.framework,systemConfiguration.framework 

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

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



* 4。现在是编码的时候了。 。 。 。 *

在您的控制器中创建一个登录按钮并为其设置操作。

  //导入GTMOAuth2Authentication,GTMOAuth2ViewControllerTouch 

在这里,当用户点击SignInGoogleButtonClicked方法时, #define GoogleClientID @贴你的客户ID
#define GoogleClientSecret @粘贴你的客户机密
#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;
$ b $ 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委托:self
finishedSelector :@selector(的viewController:finishedWithAuth:错误:)];

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




$ b //当认证完成时调用这个方法

- (void)viewController:(GTMOAuth2ViewControllerTouch *)viewController finishedWithAuth:(GTMOAuth2Authentication *)验证错误:(NSError *)错误
{

if(error!= nil){

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Google授权错误
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];
}
else
{

UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@Alert!
消息:@成功
委托:无
cancelButtonTitle:@OK
otherButtonTitles:nil];
[alert show];

}
}


I am working on an ios Application, recently I have got a new requirement for it i.e. to provide user a option to sign in with gmail . when user hit the sign in button then i want to open gmail login screen and after user enter his credentials , if the credentials are correct then instead of open his mail , i want the control to be navigate to my application home page . can anybody tell me how to achieve this

解决方案

Finally i found the solution . . .i think this will help anybody else Follow the below steps to integrate gmail with your application .

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

you will get these classes here : https://github.com/jonmountjoy/Force.com-iOS-oAuth-2.0-Example

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

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.

2. add the following frameworks

security.framework , systemConfiguration.framework

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

Then go to ApiAccess section , create client id for iOS app . then you will get clientID, ClientSecret and RedirectUrl

*4. 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 .

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

    }
}

这篇关于gos集成在ios应用程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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