NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *dir = [path objectAtIndex:0];
NSString *filePath = [dir stringByAppendingPathComponent:@"my.plist"];
NSMutableDictionary *localDictionary;
NSUrl *remoteUrl = [NSURL URLWithString:@"http://server.com/my.plist"];
NSMutableDictionary *remoteDictionary = [NSMutableDictionary dictionaryWithContentsOfURL:remoteUrl];
if(remoteDictionary != nil) {
[remoteDictionary writeToFile:filePath atomically:YES];
localDictionary = remoteDictionary;
}
else {
localDictionary = [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
if(localDictionary == nil) localDictionary = [NSMutableDictionary dictionary];
}
This will try to obtain a copy of my.plist
from http://server.com/
, on success it will write it to the disk and point localDictionary
to it, on error it will try to read from disk (e.g. downloaded earlier when there was a connection), if there is no file on disk it will create a empty NSMutableDictionary
. Either way localDictionary
will point to a NSMutableDictionary
in the end.
This is very simple and will request the file every time the code is run. You could e.g. use NSFileModificationDate
(obtained by NSFileManger attributesOfItemAtPath:error:
) and NSDate
to determine if a update is necessary. Or for big files versioning would make sense as well, having a small file containing the version of the bigger file - getting the small file from the server (e.g. with NSString
), checking whether the cached version file contains a lower version number, if so get the big file. It always depends on how often the content is refreshed.
See full documentation: NSDictionary, NSString, NSFileManager, NSDate