Reputation: 12149
I've been exploring [_webView stringByEvaluatingJavaScriptFromString:@"document.all[0].outerHTML"]
but this leaves the !doctype tag.
I'm trying to avoid stringWithContentsOfURL:
as it makes a new call.
Is there any way I can fetch the complete source of a UIWebView including !doctype and html tags?
Upvotes: 0
Views: 189
Reputation: 27620
There is no way to get the Doctype and the rest of the HTML code in one method call. However you can build the Doctype with Javascript like this (credit to Rob W for this Javascript solution):
var node = document.doctype;
var html = "<!DOCTYPE "
+ node.name
+ (node.publicId ? ' PUBLIC "' + node.publicId + '"' : '')
+ (!node.publicId && node.systemId ? ' SYSTEM' : '')
+ (node.systemId ? ' "' + node.systemId + '"' : '')
+ '>';
So put this Javascript Code into an NSString and call stringByEvaluatingJavaScriptFromString:
with it. That should give you the !Doctype. Then you can add the rest of the HTML as you described in your question.
However I'd like to suggest a second approach: Load the contents of the URL into an NSString first by using stringWithContentsOfURL:
and then load the NSString into the UIWebView using loadHTMLString:baseURL:
With that approach you get the real full HTML as a string (including the actual !Doctype and html tags) and you only use 1 request. Just make sure that you use the correct baseURL so that CSS and JS files and referenced images are loaded correctly.
Upvotes: 1