0
我目前正在使用Objective-C開發IOS應用程序。我遇到的問題是,我需要在每個視圖控制器中顯示jsondata的用戶名。 (類似Facebook的,當你登錄,您的全名/用戶名會顯示上)
在每個視圖控制器中顯示用戶名jsondata
我使用sbjson V3.1.1框架從Github在我的應用程序執行用戶身份驗證。我如何從jsondata調用用戶名數據? (我有1,用戶名,密碼和用戶名在我的jsondata陣列中)
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!"];
}
}
我試過了你的代碼,但現在我的標籤文本變空了。我認爲你的代碼正在工作,也許我的語法錯了。 –
' - (void)viewDidLoad {super viewDidLoad]; //加載視圖後,通常從筆尖執行任何其他設置。 NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults]; NSString * userName = [defaults objectForKey:@「userName」]; // NSLog(userName); usernamelabel.text = userName;' 這個viewdidload是從我的主頁。 –
確保你同步並且如果你可以在設置標籤的地方發佈代碼。 – Yan