I suspect you are trying to get the old html, your page has not finished loading yet, so it returns an empty string.
Try creating and assigning a UIWebViewDelegate
to your view, which implements webViewDidFinishLoad:
and puts your code above in this function - oldHTML should be non-empty at that point.
As for the best way, you can add content through javascript something like this:
NSString *injectSrc = @"var i = document.createElement('div'); i.innerHTML = '%@';document.documentElement.appendChild(i);"; NSString *runToInject = [NSString stringWithFormat:injectSrc, @"Hello World"]; [myWebView stringByEvaluatingJavascriptFromString:runToInject];
I would recommend clearing it a bit, as it is not fully protected, but it needs to understand how to use javascript to introduce new elements to the page.
source share