简体   繁体   中英

User defaults not saving

I just built my first app and there is only one thing left to do. I am getting data from a url. If that URL fails or is nil then I would like to get the data from the User Defaults.

I have a function that saves the user defaults from the data off the URL

- (void)saveToUserDefaults
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:data forKey:@"data"];
    [defaults synchronize];
}

and here is some more of my code.

NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
        data = [NSData dataWithContentsOfURL:url options:0 error:nil];
        [self saveToUserDefaults];
        if(url == nil){
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            data = [defaults dataForKey:@"data"];
        }
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

I disconnected from the internet and ran my app in a simulator and my app returned empty values.

What can I do to fix this?

Try this for your scenario

// method used to check network is available or not

  - (BOOL)isNetworkAvailable
    {
   BOOL connected;
const char *host = "www.apple.com";
SCNetworkReachabilityRef reachability = SCNetworkReachabilityCreateWithName(NULL, host);
SCNetworkReachabilityFlags flags;
connected = SCNetworkReachabilityGetFlags(reachability, &flags);
isConnected = NO;
isConnected = connected && (flags & kSCNetworkFlagsReachable) && !(flags & kSCNetworkFlagsConnectionRequired);
CFRelease(reachability);
    return isConnected;
}

}

now try to check

        NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
                data = [NSData dataWithContentsOfURL:url options:0 error:nil];

        bool networkStatus = [self isNetworkAvailable];
                if(networkStatus){
                     [self saveToUserDefaults];
                }
    else
    {
     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                    data = [defaults objectForKey:@"data"];
    }
                NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

try this

- (void)saveToUserDefaults
{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults setObject:data forKey:@"data"];

}

NSURL *url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];
        data = [NSData dataWithContentsOfURL:url options:0 error:nil];
        [self saveToUserDefaults];
        if(url == nil){
            NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
            data = [defaults objectForKey:@"data"];
        }
        NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

Try this one.

NSURL * url = [NSURL URLWithString:@"http://jamessuske.com/isthedomeopen/isthedomeopenGetData.php"];

NSURLRequest * request = [NSURLRequest requestWithURL:url];

NSOperationQueue * newQueue = [[NSOperationQueue alloc] init];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:newQueue
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                               dispatch_async(dispatch_get_main_queue(), ^{

                                   NSArray * array = nil;

                                   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

                                   if (error == nil) {

                                       array = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];

                                       [defaults setObject:array forKey:@"data"];

                                   } else {

                                       array = [defaults objectForKey:@"data"];

                                   }

                                   NSLog(@"%@", array);

                               });

                           }];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM