Reputation: 1753
I'm developing an iOS app which gets its data from a json feed of my server. Parsing the string is no problem... However, I am having trouble downloading the string asynchronously and then caching it. I found SDURLCache but do not know how to implement it. What is the best way to do this.
Upvotes: 2
Views: 1040
Reputation: 1771
You could download the file to disk (synchronously):
NSURL * url = [NSURL URLWithString:@"www.yourprovider.com/your.json";
NSData *file = [NSData dataWithContentsOfURL:url];
[file writeToFile:<your file path> atomically:YES];
For asynchronous operations, instead, you should use NSURlConnection. After opening the connection,
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:<your NSURL>];
[request setHTTPMethod:@"GET"];
NSURLConnection *conn = [NSURLConnection connectionWithRequest:request delegate:self];
[conn start];
you receive data in this call back:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)_data {
//here you could write to a NSFileHandle ivar:
if (file) {
[file seekToEndOfFile];
} [file writeData:_data];
}
Upvotes: 2