如何使用从 URL 获取的 NSUserDefaults 存储数据并将它们设置为 textFields

How to store data using NSUserDefaults which is fetched from URL and set them to textFields

在下面的代码中,我从 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"];
    }
}

商店:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setValue:nameTextView.text forKey:@"name"];
[defaults synchronize];

获取:

NSString *value = [[NSUserDefaults standardUserDefaults] stringForKey:@"name"];

首先获取 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;
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"];