-4

Below is my code,I am using to get data from google account,I want to use email address in other view as login id.

This code is in my FirstViewController and I want to use email in another view.

Please Help :)

- (void)finishedWithAuth:(GTMOAuth2Authentication *)auth
               error:(NSError *)error {
if (error) {
    return;
}


NSString  *accessTocken = [auth valueForKey:@"accessToken"]; // access tocken pass in .pch file

NSLog(@"%@",accessTocken);
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];
NSDictionary *jsonDictionary = [NSJSONSerialization JSONObjectWithData:[jsonData dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingMutableContainers error:&error];
NSString *userId=[jsonDictionary objectForKey:@"email"];

NSLog(@" user data %@",jsonData);
NSLog(@"Received Access Token:%@",auth);
NSLog(@"email id : %@ ",userId);

GTLServicePlus* plusService = [[GTLServicePlus alloc] init];
plusService.retryEnabled = YES;
[plusService setAuthorizer:auth];
GTLQueryPlus *query = [GTLQueryPlus queryForPeopleGetWithUserId:userId];

[plusService executeQuery:query completionHandler:^(GTLServiceTicket *ticket,GTLPlusPerson *person,NSError *error) {
    if (error) {
        GTMLoggerError(@"Error: %@", error);
    } else {
        // Retrieve the display name and "about me" text
       // [person retain];
        NSString *description = [NSString stringWithFormat:
                                 @"%@\n%@", person.displayName,
                                 person.aboutMe];
        GTLPlusPersonImage *image  =person.image;
        NSString *strimag=[image valueForKey:@"url"];

        // [self setImageFromURL:[NSURL URLWithString:strimag]];
        NSData *receivedData = [NSData dataWithContentsOfURL:[NSURL URLWithString:strimag]];

        UIImage *img = [[UIImage alloc] initWithData:receivedData ];
        receivedData=UIImageJPEGRepresentation(img,50);
        NSLog(@"description %@",description);
    }
}];
 if ([GPPSignIn sharedInstance] == true) {
   [self performSegueWithIdentifier:@"pass" sender:self];
      }

  [[GPPSignIn sharedInstance] signOut];
 }
Nirav D
  • 71,513
  • 12
  • 161
  • 183
Shikha Sharma
  • 139
  • 1
  • 14
  • Can you show use code of prepareforsegue or can you please give name of your destination ViewController? – Nirav D Sep 14 '16 at 10:26
  • @NiravD CreatePasswordViewController it is and i have to navigate to this when values from google account is fetched and i want to use email to create new account. – Shikha Sharma Sep 14 '16 at 10:46

1 Answers1

1

Here are some ways to save data inside application.

  1. create local database using sqlite or coredata both provides facilty to save data locally and before use please find the different situations to use these databases.
  2. using NSUserDefaluts but not recomemded because NSUserDefaults aren’t meant to store sensitive information for more information see imp link see example also if you still.

To store data using NSUserDefaluts:

[[NSUserDefaults standardUserDefaults]setObject:self.textfield.text forKey:@"yourKey"];

To get data anywhere inside app:

object = [[NSUserDefaults standardUserDefaults] valueForKey:@"yourKey"];
vaibhav
  • 4,038
  • 1
  • 21
  • 51