Is there an alternative to ASIWebPageRequest in the ASIHTTPRequest library that I can use for downloading an entire web page in iOS including the CSS, JavaScript and image files, etc? I can't seem to find a similar class in the AFNetworking framework and so far my searches haven't been successful. I can't use ASIHTTPRequest as I can't seem to get it to work at all in any of my apps, no examples I've found work for iOS7 and I'd much rather use something more recent anyway.
I basically want to store an entire webpage locally in a directory on the iPhone/iPad so that a user can then edit it locally and send the whole directory to their web server later. The user also needs to be able to view the webpage at any time in a UIWebView.
If this isn't possible I'll have to just download the HTML file and then parse it to find the URLs of external resources, then download those separately. I'd much rather not do this, but if I have to then what's the best library for accomplishing this?
Thanks to anyone to helps me out!
It seems you are after something like wget
but I'm not sure if you can do that in iOS.
You can use the native method NSString stringWithContentsOfURL:encoding:error:
to fetch the HTML of a web page.
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSString_Class/Reference/NSString.html#//apple_ref/occ/clm/NSString/stringWithContentsOfURL:encoding:error:
You can intercept all of the HTTP requests made from your app via a custom NSURLProtocol. You'll have to expand on this concept, but here's a simple class that when dropped into your project will log out all of the requests being made (e.g. from a UIWebView).
@interface TSHttpLoggingURLProtocol : NSURLProtocol
@end
@implementation TSHttpLoggingURLProtocol
+ (void) load
{
[NSURLProtocol registerClass: self];
}
+ (BOOL) canInitWithRequest: (NSURLRequest *) request
{
NSLog( @"%@", request.URL );
return NO;
}
@end
You could expand on this and actually perform the requests from the protocol implementation (see https://github.com/rnapier/RNCachingURLProtocol for a more complete example). Or you could just track the requests and download them yourself later.
There's no easy way that I know of to associate a protocol handler like this with a particular instance of a UIWebView. You're going to intercept ALL of the requests in the app with this. (Caveat - in iOS7, protocols can be registered to specific NSURLSessions; for these I think a globally registered protocol handler won't intercept.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With