在下面的代码中,我从URL获取数据并直接将其存储到TextFields
。但是,当我的应用程序长时间进入后台并且我从暂停状态重新打开时,数据会丢失。
有没有人知道如何在NSUserDefaults
中获取和存储数据,所以我不会丢失它,然后在文本字段中显示它?
- (void)viewDidLoad {
[super viewDidLoad];
NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://190.179.4.9:92/mybite_joomla/cron/webapi.php?task=webapi.viewProfile"]];
[request setHTTPMethod:@"GET"];
[request setValue:@"application/json;charset=UTF-8" forHTTPHeaderField:@"content-type"];
NSError *err;
NSURLResponse *response;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];
for (NSDictionary *dict in jsonArray) {
// here I am directly setting it into textfields and I need to store it first
nameTextView.text =[jsonArray valueForKey:@"name"];
usernameTextView.text =[jsonArray valueForKey : @"username"];
emailTextView.text=[jsonArray valueForKey : @"email"];
cityTextView.text =[jsonArray valueForKey : @"city"];
stateTextView.text =[jsonArray valueForKey : @"state"];
countryTextView.text =[jsonArray valueForKey : @"country"];
}
}
答案 0 :(得分:0)
商店:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:nameTextView.text forKey:@"name"];
[defaults synchronize];
得到:
NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];
答案 1 :(得分:0)
首先获取NSUserDefaults的实例:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
然后你可以存储数据:
NSString *name =[jsonArray valueForKey:@"name"];
[defaults setObject:name forKey:@"name"];
[defaults synchronize];
获取数据:
NSString *storedName = [defaults stringForKey:@"name"];
nameTextView.text = storedName;
答案 2 :(得分:0)
for (NSDictionary *dict in jsonArray) {
[[NSUserDefaults standardUserDefaults]setObject:[jsonArray valueForKey:@"name"] forKey:@"nameString"];
[[NSUserDefaults standardUserDefaults]setObject:[jsonArray valueForKey:@"username"] forKey:@"usernameString"];
[[NSUserDefaults standardUserDefaults]setObject:[jsonArray valueForKey:@"email"] forKey:@"emailString"];
[[NSUserDefaults standardUserDefaults]setObject:[jsonArray valueForKey:@"city"] forKey:@"cityString"];
[[NSUserDefaults standardUserDefaults]setObject:[jsonArray valueForKey:@"state"] forKey:@"stateString"];
[[NSUserDefaults standardUserDefaults]setObject:[jsonArray valueForKey:@"country"] forKey:@"countryString"];
}
然后,在viewWillAppear中:
nameTextView.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"nameString"];
usernameTextView.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"usernameString"];
emailTextView.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"emailString"];
cityTextView.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"cityString"];
stateTextView.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"stateString"];
countryTextView.text = [[NSUserDefaults standardUserDefaults] stringForKey:@"countryString"];