I want to load a webpage when user connected to network and store it offline(including with images/resources). If the user not connected to any network then i should load the previously stored webpage. I have tried NSData dataWithContentsOfURL:(NSURL *)url
and NSString stringWithContentsOfURL
but these stores only html content not the resources.
Thanks in advance.

- 295
- 2
- 18
4 Answers
You can do that with ASIHttpRequest. If you do not want to to use that project (it is no longer active) you can look into the code and what it does. Look at "how to cache a whole web page with images in iOS" for more info as well.
Write this data into file using:
-(void)writeDataToFile:(NSString*)filename
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
if(filename==nil)
{
DLog(@"FILE NAME IS NIL");
return;
}
// the path to write file
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat: @"%@",filename]];
/*NSData *writeData;
writeData=[NSKeyedArchiver archivedDataWithRootObject:pArray]; */
NSFileManager *fm=[NSFileManager defaultManager];
if(!filePath)
{
//DLog(@"File %@ doesn't exist, so we create it", filePath);
[fm createFileAtPath:filePath contents:self.mRespData attributes:nil];
}
else
{
//DLog(@"file exists");
[self.mRespData writeToFile:filePath atomically:YES];
}
NSMutableData *resData = [[NSMutableData alloc] init];
self.mRespData=resData;
[resData release];
}
and load it next time.
-
I don't think that this answers the question. It's not about how to store data, but about how to download everything of website and store it. – Kai Huppmann May 08 '12 at 12:16
-
I don't want to store any data, but the questioner seems to ... The question - as I understood - is: How to save *everything* of *any* website for later offline usage. – Kai Huppmann May 08 '12 at 12:21
I think the simple solution is this - "Safari Client-Side Storage and Offline Applications Programming Guide", https://developer.apple.com/library/archive/documentation/iPhone/Conceptual/SafariJSDatabaseGuide/Introduction/Introduction.html
Only if you are making an app with HTML5 and webview, didn't test this method yet so far, so it might work.
-
This is about offline storage, if you are the web developer, but the question is how to store *any* website for offline usage. – Kai Huppmann May 08 '12 at 12:18
-
Thank you for telling me that, You are right, only if we are making an app with webview and HTML5 as well, HTML5 can do this job. – Tom May 08 '12 at 13:15
I don't know if there is one-line-solution like myWebView.cache4Offline = YES;
, but I fear as long as you don't have access to the website's code (i.e. if you want to make any website available offline inside your app), you have to program this on your own. Thinking about it, it doesn't seem so difficult:
- Scan the html string for image urls (and everything else you need)
- Download those resources from the internet using
NSData dataWithContentsOfURL
(maybe a little annoying, because of relative/absolute URLs) - Save data to file with NSData writeToFile:options:error:
- Replace URL in HTML with filePath from 3. (OR, better use a convention for converting their URLs in your file-URLs)
Hope it helps

- 9,289
- 12
- 69
- 108

- 10,705
- 6
- 47
- 78
-
This solution would work, except that you have no way to replace the URL with the filepath in hTML, since the filePath points to NSData not a .png image – Oscar Gomez Feb 04 '13 at 20:49