Quero fazer cache do JSON API do meu Wordpress em meu app. Quero um cache dos tableview cells
e UIWebView
para quando estiver em modo avião ou desligado ou com mau sinal, o usuário ainda possa ver. Eu tentei fazer o cache com AFNetworking e NSURLCache mas não sei como. Como posso formatar AFNetworking ou NSURLCache para isto?
NSURL *blogURL =[NSURL URLWithString:@"http://purpledrop.org/api/get_recent_summary/"];
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDownloadTask *task = [session downloadTaskWithURL:blogURL completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
NSData *jsonData = [[NSData alloc] initWithContentsOfURL:location];
//this app would crash without this code below in airplane mode or no wifi
if (jsonData ==nil) {
return;
}
NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
self.blogPosts = [NSMutableArray array];
NSArray *blogPostArray = [dataDictionary objectForKey:@"posts"];
for (NSDictionary *bpDictionary in blogPostArray) {
BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]];
blogPost.author = [bpDictionary objectForKey:@"author"];
blogPost.thumbnail = [bpDictionary objectForKey:@"thumbnail"];
blogPost.date = [bpDictionary objectForKey:@"date"];
blogPost.url = [NSURL URLWithString:[bpDictionary objectForKey:@"url"]];
[self.blogPosts addObject:blogPost];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}];
[task resume];
}