Objective c, downloading, checking and keeping available a plist file
Okay, I'm having a bit of a mental block today/this week, so I'm probably being stupid but I can't get this to work. So, any advice/help as always would be hugely appreciated...
I need my iPhone app, on request to download a plist file from a server just once and have it available any time the app loads up (IE it only needs to be downloaded once in reality). But I really don't think I am doing it right... I'm using ASIHTTPRequest to get the file and this is running in a separate thread to allow a progress bar to update. The download is started either on first run or on request (at the moment).
I am not getting any errors and the serve开发者_如何学JAVAr is being contacted, however I don't know if/where the file is being saved to. My tests seem to suggest the file is not getting downloaded.
So I guess my questions are:
- Where should I save the file? Are there any rules to this or will it be held within the app itself on the phone?
- Will this file be available any time the app is run once it is downloaded?
- Is there a better way to do this?
Some code below:
-(void)getPlist
{
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [docpaths objectAtIndex:0];
NSURL *url = [NSURL URLWithString:plistURL];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:documentsDirectory];
[request setDownloadProgressDelegate:progbar];
[request startSynchronous];
}
Thanks in advance/apologies if this is stupidly n00bish, still very much getting the hang of this Objective-C business.
If you're using ASIHTTPRequest just use it's buildin caching system. Don't rollout your own. It's quite simple and easy to manage
ASIHTTPRequest can automatically store downloaded data in a cache for use later. This can be helpful in many situations:
You want to have access to the data when there is no internet connection and you can’t download it again You want to download something only if it has changed since you last downloaded it The content you are working with will never change, so you only want to download it once
http://allseeing-i.com/ASIHTTPRequest/How-to-use#using_a_download_cache
To answer your orignal question(s)
Where should I save the file? Are there any rules to this or will it be held within the app itself on the phone?
Just save it to the documents directory of your applications. Its sandboxed.
Will this file be available any time the app is run once it is downloaded?
Yes. But if the user deleted your app and reinstalled it all your cached files will be deleted. Handle the situation.
Is there a better way to do this?
I don't see anything wrong with your current implementation. If you run into problems check your request object for error and/or check the return HTTP StatusCode int statusCode = [request responseStatusCode];
Get your documents path to your (internal on your device) path:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
I think the download destination path needs to be a path to a file, not just the directory to save the file in. Try this:
-(void)getPlist
{
NSArray *docpaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [docpaths objectAtIndex:0];
// path to the new destination file
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"foo.bar"];
NSURL *url = [NSURL URLWithString:plistURL];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setDownloadDestinationPath:path]; // use the file path
[request setDownloadProgressDelegate:progbar];
[request startSynchronous];
}
精彩评论