我怎样才能将从 json 检索到的数据从一个视图传递到另一个视图?
How can i pass data retrieved from json from one view to another view?
下面是我的代码,我正在使用从 google 帐户获取数据,我想在其他视图中使用电子邮件地址作为登录 ID。
此代码在我的 FirstViewController
中,我想在另一个视图中使用电子邮件。
请帮忙:)
- (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];
}
以下是在应用程序中保存数据的一些方法。
- 使用
sqlite
或 coredata
创建本地数据库都提供了在本地保存数据的便利,使用前请找出不同的情况来使用这些数据库。
- 使用 NSUserDefaults 但不推荐使用,因为 NSUserDefaults 并不意味着存储敏感信息以获取更多信息,请参阅 imp link 如果您仍然需要,请参阅示例。
使用 NSUserDefaluts 存储数据:
[[NSUserDefaults standardUserDefaults]setObject:self.textfield.text forKey:@"yourKey"];
要在应用内的任何位置获取数据:
object = [[NSUserDefaults standardUserDefaults] valueForKey:@"yourKey"];
下面是我的代码,我正在使用从 google 帐户获取数据,我想在其他视图中使用电子邮件地址作为登录 ID。
此代码在我的 FirstViewController
中,我想在另一个视图中使用电子邮件。
请帮忙:)
- (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];
}
以下是在应用程序中保存数据的一些方法。
- 使用
sqlite
或coredata
创建本地数据库都提供了在本地保存数据的便利,使用前请找出不同的情况来使用这些数据库。 - 使用 NSUserDefaults 但不推荐使用,因为 NSUserDefaults 并不意味着存储敏感信息以获取更多信息,请参阅 imp link 如果您仍然需要,请参阅示例。
使用 NSUserDefaluts 存储数据:
[[NSUserDefaults standardUserDefaults]setObject:self.textfield.text forKey:@"yourKey"];
要在应用内的任何位置获取数据:
object = [[NSUserDefaults standardUserDefaults] valueForKey:@"yourKey"];