I have a hybrid (Objective C + HTML) application and I would like to be able to periodically save a remote file (images and css) to a user’s device but in such a way that it can be loaded by a browser within a webview instance in the same application.
I don’t think it’s possible to save files to the resource bundle itself (though in the simulator you can), so I assume I would have to save the file somewhere else. But I’m not sure what path to use where the HTML document could still access it.
Basically, I’d like to do something like this:
NSURL* url = [NSURL URLWithString:@"http://myserver.com/August.png"];
NSString* filePath = @"[?????]/MonthlyImage.png";
NSData* data = [NSData dataWithContentsOfURL:url];
if (data)
{
NSError* error;
if ([data writeToFile:filePath options:NSDataWritingAtomic error:&error])
{
NSLog(@"Wrote");
}
if (error != nil)
{
NSLog(@"ERROR: %@", [error description]);
}
}
Then in my webview, I’d like to be able to load the image like this:
<img src="[??????]/MonthlyImage.png" />
What values can I use in place of the ?????? that will work? Is it even possible?
Saving to the bundle is not allowed because it is not allowed to modify the app binary.
You could try and store the file in the documents folder, then retrieve it from there. The important point, to make it work, is specifying also a
baseURLwhen creating theUIWebVIew.You can retrieve the documents directory in this way:
Then you store there your file, and when you want to load it in your
UIWebView, execute:Now,
<img src="MonthlyImage.png" />will look for the png in your documents directory.