I am trying to archive my UIWebView
object using NSKeyedArchiver
and restore it later using NSKeyedUnarchiver
in the same state. Archiving/Unarchiving works, but the UIWebView
is blank when unarchived again (I archived it with a website loaded), but I need it to have the same website it did before I archived it.
I've tried saving the HTTPBody
of the NSURLRequest
of the UIWebView
, but that always returns (null)
, so I think I am barking up the wrong tree there. (or I am fetching it wrong?)
Here's what I have tried:
UIWebView*currentWebview = [[Controller sharedInstance] mainWebView];
NSData*webViewData开发者_StackOverflow = [NSKeyedArchiver archivedDataWithRootObject:currentWebview];
[sub setObject:webViewData forKey:@"Instance"]; //saves and decodes correctly, but that's a blank `UIWebView` even though I saved it with contents being loaded
NSData*body = [[currentWebview request] HTTPBody]; //returns (null) always, but the webview is not null and has a valid request
[sub setObject:body forKey:@"HTTP_Body"];
What am I missing here? I need to retain the UIWebView
as it was and load it again in the same state without having to open the URL at a later stage.
And yes, I am aware of document.documentElement.outerHTML
and document.documentElement.innerHTML
, but that's not what I am looking for. I need to retain the whole website for later, not just the HTML code.
Thanks!
You're trying to do something that NSKeyedArchiver just won't do. Sorry! If you want to archive a web page you have to do it yourself and then reload the page when you recreate the web view.
To get the websites data you need to do the loading yourself (using NSURLConnection etc) and store it as it arrives.
I'd take a look at the ASIHTTP library - it replaces NSURL... methods with (IMHO) a much nicer and more flexible set of objects. They've just added a new method that should do exactly what you're trying to do - it's called ASIWebPageRequest.
Hope that helps.
You may have a look at this S.O. topic.
It will not be really useful for you, but it seems that other people have tried it without much success.
You can use this code: https://github.com/rnapier/RNCachingURLProtocol simple to implement, works perfectly
精彩评论