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?