我在我的IOS应用程序中集成了google plus,但我无法获取登录的当前用户的电子邮件ID

Vis*_*tri 1 iphone ios google-plus-one google-plus ios6

我已经在我的ios应用中集成了google plus,我能够成功登录,但我无法获取当前用户登录的电子邮件ID.我已经参考了https://developers.google.com/+/ mobile/ios /并遵循登录所需的所有步骤!

那么,我如何获得登录Google Plus的当前用户邮件ID?

在此输入图像描述

小智 8

转到GTMOAuth2Authentication.m文件方法,dic中的setKeysForResponseDictionary在方法结束时返回访问令牌.

accessTocken = [dict valueForKey:@"access_token"]; // access tocken pass in .pch file
[accessTocken retain];
Run Code Online (Sandbox Code Playgroud)

并在你的控制器

- (IBAction)momentButton:(id)sender {
  NSString *str =  [NSString stringWithFormat:@"https://www.googleapis.com/oauth2/v1/userinfo?access_token=%@",accessTocken];
  NSString* escapedUrl = [str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
  NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",escapedUrl]];
  NSString *jsonData = [[NSString alloc] initWithContentsOfURL:url usedEncoding:nil error:nil];
  NSMutableDictionary *proDic = [[NSMutableDictionary alloc] init];

  proDic=[jsonData JSONValue];
  NSLog(@"%@",proDic);
Run Code Online (Sandbox Code Playgroud)


Muh*_*din 5

这是获取当前登录用户的电子邮件ID的最简单和最简单的方法,
首先创建GPPSignIn类的实例变量

GPPSignIn *signIn;
Run Code Online (Sandbox Code Playgroud)

然后在中初始化它 viewDidLoad

- (void)viewDidLoad
  {
  [super viewDidLoad];

   static NSString * const kClientID = @"your client id";
   signIn = [GPPSignIn sharedInstance];
   signIn.clientID= kClientID;
   signIn.scopes= [NSArray arrayWithObjects:kGTLAuthScopePlusLogin, nil];
   signIn.shouldFetchGoogleUserID=YES;
   signIn.shouldFetchGoogleUserEmail=YES;
   signIn.delegate=self;

   }
Run Code Online (Sandbox Code Playgroud)

接下来GPPSignInDelegate在您的视图控制器中实现
您可以获取登录用户的电子邮件ID

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
               error:(NSError *)error
 {  
  NSLog(@"Received Access Token:%@",auth);
   NSLog(@"user google user id  %@",signIn.userEmail); //logged in user's email id
  }
Run Code Online (Sandbox Code Playgroud)