gpt4 book ai didi

ios - 在每个 View Controller 中显示用户名 jsondata

转载 作者:行者123 更新时间:2023-11-29 01:27:47 25 4
gpt4 key购买 nike

  1. 我目前正在使用 Objective-C 开发 IOS 应用程序。我遇到的问题是,我需要在每个 View Controller 中显示 jsondata 中的用户名。 (就像facebook一样,当您登录时,您的全名/用户名将显示在上面)

  2. 我正在使用 Github 中的 sbjson v3.1.1 框架在我的应用程序中执行用户身份验证。如何从 jsondata 调用用户名数据? (我的 jsondata 数组中有 1、userid、密码和用户名)

3.这是我的身份验证代码。

@try {

if([[txtLogin text] isEqualToString:@""] || [[txtPassword text] isEqualToString:@""] ) {
[self alertStatus:@"Please enter both Username and Password" :@"Login Failed!"];
} else {
NSString *post =[[NSString alloc] initWithFormat:@"userid=%@&pass=%@",[txtLogin text],[txtPassword text]];
NSLog(@"PostData: %@",post);

NSURL *url=[NSURL URLWithString:@"http://192.168.1.14:8888/itrack/mobile/login.php?"];

NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

//[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]];

NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *response = nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

NSLog(@"Response code: %d", [response statusCode]);
if ([response statusCode] >=200 && [response statusCode] <300)
{
NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
NSLog(@"Response ==> %@", responseData);

SBJsonParser *jsonParser = [SBJsonParser new];
NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil];
NSLog(@"%@",jsonData);
NSInteger success = [(NSNumber *) [jsonData objectForKey:@"login"] integerValue];
NSLog(@"%d",success);

// NSString *test = (NSString *) [jsonData objectForKey:@"username"];


if(success == 1)
{
NSLog(@"Login SUCCESS");
[self alertStatus:@"Logged in Successfully." :@"Login Success!"];

homepageVC *controller = [self.storyboard instantiateViewControllerWithIdentifier:@"homeEN"];
[self presentViewController:controller animated:YES completion:nil];

//redirect to homepage
// UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"main" bundle:nil];
//UIViewController *newView = [mainStoryboard instantiateViewControllerWithIdentifier:@"login"];
//[self presentViewController:newView animated:YES completion:nil];


} else {

NSString *error_msg = (NSString *) [jsonData objectForKey:@"err"];
[self alertStatus:error_msg :@"Login Failed!"];
}

} else {
if (error) NSLog(@"Error: %@", error);
[self alertStatus:@"Connection Failed" :@"Login Failed!"];
}
}
}
@catch (NSException * e) {
NSLog(@"Exception: %@", e);
[self alertStatus:@"Login Failed." :@"Login Failed!"];
}
}

最佳答案

其中一种方法是将用户名存储在 NSUserDefaults 中,然后在每个其他 View Controller 中检索。这样,如果用户名 key 不为零,那么您就知道该用户确实进行了身份验证。

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:"TheName" forKey:@"username"];
// you can store any other properties if neeeded
[defaults synchronize]; // to save the data into NSUserDefaults

要在任何其他 View Controller 中检索用户名,即使应用已重新启动也是如此

 NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userName = [defaults objectForKey:@"userName"];

让我知道这是否适用于您的情况。

关于ios - 在每个 View Controller 中显示用户名 jsondata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33817765/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com