iphone - iOS 中的 Gmail 集成示例

标签 iphone ios objective-c

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

最佳答案

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

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

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

GTMHTTPFetcher.h , GTMHTTPFetcher.m

GTMOAuth2Authentication.h, GTMOAuth2Authentication.m

GTMOAuth2SignIn.h,GTMOAuth2SignIn.m

GTMOAuth2ViewControllerTouch.h,GTMOAuth2ViewControllerTouch.m,

GTMOAuth2ViewTouch.xib

SBJSON.h , SBJSON.m

您将获得这些类(class) 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. 现在是编码时间。 . . .create a signIn button in your controller and set the action for that。这里当用户点击按钮时调用 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]

    }
}

关于iphone - iOS 中的 Gmail 集成示例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16828135/

相关文章:

ios - 再现键中的比例必须与图像描述中传递的比例相匹配

iphone - iOS应用中的线程问题

iOS 5 上的 jQuery 事件

iphone - 如何获取应用程序启动 iPhone 的次数

ios - 设置隐藏不工作

iphone - facebook-ios-sdk 注销

iphone - 访问自定义 MKAnnotationView 的 "selected"属性?

ios - Visual Studio 2015/Xamarin,调试不工作 : an error occurred while executing MTouch

ios - 加载以前从相机胶卷中保存的图像

ios - 圆形,3 色渐变,动画进度 View