Load an entire web page in iOS WITHOUT using ASIHTTPRequest

Is there an alternative to ASIWebPageRequest in the ASIHTTPRequest library that I can use to load an entire web page in iOS, including CSS, JavaScript and image files, etc.? I cannot find a similar class in the AFNetworking structure, and so far my searches have not been successful. I can’t use ASIHTTPRequest, since I can’t get it to work in any of my applications at all, no examples I found for iOS7, I would rather use something more recent.

Basically, I want to store the entire web page locally in a directory on the iPhone / iPad, so that later the user can edit it locally and later send the entire directory to their web server. The user should also be able to view the web page at any time in UIWebView.

If this is not possible, I just need to download the HTML file and then parse it to find the URLs of external resources and then download them separately. I would rather not do this, but if I need to, what is the best library for this?

Thanks to everyone who helps me!

+8
ios objective-c uiwebview afnetworking
source share
3 answers

You seem to be after something like wget , but I'm not sure if you can do this in iOS.

You can use the NSString stringWithContentsOfURL:encoding:error: native method NSString stringWithContentsOfURL:encoding:error: to retrieve the HTML page of the 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 :

+2
source

I would use a UIWebView and cache all the requests it makes

+1
source

You can intercept all HTTP requests from your application through a custom NSURLProtocol. You will have to expand this concept, but here is a simple class that, when you enter your project, will exit all requests (for example, UIWebView).

 @interface TSHttpLoggingURLProtocol : NSURLProtocol @end @implementation TSHttpLoggingURLProtocol + (void) load { [NSURLProtocol registerClass: self]; } + (BOOL) canInitWithRequest: (NSURLRequest *) request { NSLog( @"%@", request.URL ); return NO; } @end 

You can extend this and actually execute requests from the protocol implementation (see https://github.com/rnapier/RNCachingURLProtocol for a more complete example). Or you can simply track requests and download them yourself later.

There is no easy way that I know to associate a protocol handler like this with a specific instance of UIWebView. With this, you will intercept ALL requests in the application. (Caveat - in iOS7, protocols can be registered in certain NSURLSessions, for them, I think that a protocol handler registered globally will not intercept.)

+1
source

All Articles