【问题标题】:gmail integration in ios applicationios应用程序中的gmail集成
【发布时间】:2015-05-24 17:13:34
【问题描述】:

我正在开发一个 ios 应用程序,最近我有一个新要求,即为用户提供使用 gmail 登录的选项。当用户点击登录按钮时,我想打开 gmail 登录屏幕,在用户输入他的凭据后,如果凭据正确,那么我希望控件导航到我的应用程序主页,而不是打开他的邮件。谁能告诉我如何实现这一目标

【问题讨论】:

  • 嘿,您可以将 gmail 集成到您的应用程序中。请提供一个 gud 链接。我尝试了您的答案,但我收到错误 Error Domain=com.google.GTMOAuth2 Code=-1001..When I go对于此链接后的其他代码 technogerms.com/… 获取访问令牌,但无法使用该访问令牌检索用户详细信息
  • @arizah,你的邮箱是什么,我可以给你发个样品
  • 好的,请发到profile里的id
  • @Kausar 您的电子邮件 ID 在您的个人资料中不可见。
  • 好的。请发邮件到这个 hajirakausar21@gmail.com

标签: ios objective-c gmail


【解决方案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

您将在这里获得这些课程: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 作为这些文件的编译器标志。

2。添加以下框架

security.framework , systemConfiguration.framework

3.将您的应用注册到 google api 控制台……这里:https://code.google.com/apis/console

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

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

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

    }
}

【讨论】:

  • 我一步一步尝试了这个解决方案,但没有工作没有任何错误。或者也没有获得 gmail 登录窗口。
  • 伙计,代码对我有用,你能交叉检查你的代码一次吗?
  • 它工作得更好,但我收到错误 Error Domain=com.google.GTMOAuth2 Code=-1001 "The operation could not be completed. (com.google.GTMOAuth2 error -1001.)
  • 我遇到了同样的问题 Error Domain=com.google.GTMOAuth2 Code=-1001
  • 我认为你们应该检查您的#define GoogleAuthURL @"" 和#define GoogleTokenURL @"" .....这些代码不正确
【解决方案2】:

我找到了。但后来我只能获取sn-p,即电子邮件正文的前几句话而不是全部。我刚停下来,因为我没有找到任何其他方法。因为我使用的是 OAuth 2.0 。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-12
    • 1970-01-01
    相关资源
    最近更新 更多