【问题标题】:Example for Gmail Integration in iosios 中的 Gmail 集成示例
【发布时间】:2015-03-19 16:26:20
【问题描述】:

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

【问题讨论】:

  • 你得到答案了吗。

标签: iphone ios objective-c


【解决方案1】:

我终于找到了解决方案。 . . .我认为这会有所帮助

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

1.为您的项目添加以下类。

GTMHTTPFetcher.h , GTMHTTPFetcher.m

GTMOAuth2Authentication.h, GTMOAuth2Authentication.m

GTMOAuth2SignIn.h,GTMOAuth2SignIn.m

GTMOAuth2ViewControllerTouch.h,GTMOAuth2ViewControllerTouch.m,

GTMOAuth2ViewTouch.xib

SBJSON.h , SBJSON.m

您将获得这些课程here : 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 中选择项目和目标。在目标“Build Phases”选项卡下,展开 Compile Sources 构建阶段,选择库源文件,然后按 Enter 打开编辑字段,然后键入 -fno-objc-arc 作为这些文件的编译器标志。

  1. 添加以下框架

    security.framework、systemConfiguration.framework

  2. 将您的应用注册到 google api 控制台...。 here : https://code.google.com/apis/console

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

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

代码

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

    }
}

【讨论】:

  • 第二种方法,即 - (void)viewController:(GTMOAuth2ViewControllerTouch * )viewController finishedWithAuth:(GTMOAuth2Authentication * )auth error:(NSError * )error not called
  • @g212gs 它将在身份验证完成后被调用。
  • Error Domain=com.google.GTMOAuth2 Code=-1001“操作无法完成。(com.google.GTMOAuth2错误-1001。)”
  • @g212gs 发送测试邮件至 skriyaz7@gmail.com 我将为您提供工作演示。
  • @RIYAZ 感谢您的解决方案。
猜你喜欢
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-11-19
  • 2018-06-07
  • 2018-03-20
相关资源
最近更新 更多