Getting the HTML source code of a loaded UIWebView [duplicate]

青春壹個敷衍的年華 提交于 2019-12-17 17:30:47

问题


How can I get the HTML source code of a UIWebView and store it in NSString?


回答1:


If you want the source code of an already loaded webview, you can get it like this:

    NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.body.innerHTML"];

Update: method above works most of the time. But I recently was in a situation where I needed all the code source, not just the body.

Here is a better way:

    NSString *yourHTMLSourceCodeString = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"];       

(It won't reload the webview).




回答2:


I am not sure that what exactly you need..........but as per your last comment you will get that html data from google like this...

     NSData *companyData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.google.com"]];
     NSString *responseString = [[NSString alloc] initWithData:companyData encoding:NSUTF8StringEncoding];

    self.yourTextView.text = responseString; // response string contains that data
    [companyData release];

Good Luck!



来源:https://stackoverflow.com/questions/5167254/getting-the-html-source-code-of-a-loaded-uiwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!