从UIWebView中读取HTML内容 [英] Reading HTML content from a UIWebView

查看:74
本文介绍了从UIWebView中读取HTML内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以读取已加载到 UIWebView 中的原始HTML内容?



如果没有,是否有另一种方法从iPhone SDK中的网页中提取原始HTML内容(例如相当于.NET WebClient :: openRead )? 解决方案

第二个问题实际上更容易回答。查看 stringWithContentsOfURL:encoding:error : NSString的方法 - 它允许你传递一个URL作为NSURL的实例(它可以很容易地从NSString实例化),并返回一个字符串,其中包含页面的完整内容URL。例如:

  NSString * googleString = @http://www.google.com; 
NSURL * googleURL = [NSURL URLWithString:googleString];
NSError *错误;
NSString * googlePage = [NSString stringWithContentsOfURL:googleURL
encoding:NSASCIIStringEncoding
error:& error];

运行此代码后, googlePage 将包含www.google.com的HTML和 error 将包含在提取中遇到的任何错误。 (你应该在fetch之后检查 error 的内容。)

换个角度来说(从UIWebView中)有点棘手,但基本上是相同的概念。您必须从请求然后按照之前的方式进行获取:

  NSURL * requestURL = [[yourWebView request] URL]; 
NSError *错误;
NSString * page = [NSString stringWithContentsOfURL:requestURL
encoding:NSASCIIStringEncoding
error:& error];

编辑:然而,这两种方法都会带来性能问题,因为它们做两次请求。您可以通过使用它的 stringByEvaluatingJavascriptFromString:方法从当前加载的UIWebView中获取内容,如下所示:

pre> NSString * html = [yourWebView stringByEvaluatingJavaScriptFromString:
@document.body.innerHTML];

这将使用文档对象模型获取视图的当前HTML内容,解析JavaScript,然后作为HTML的NSString *提供给您。另一种方法是先以编程方式执行您的请求,然后从您请求的内容中加载UIWebView。让我们假设上面的第二个示例,其中 NSString * page 作为调用 stringWithContentsOfURL的结果:encoding:error:。然后,您可以使用 loadHTMLString:baseURL:将该字符串推送到Web视图中,假设您还保留了您请求的NSURL:

  [yourWebView loadHTMLString:page baseURL:requestURL]; 

但是,我不确定这是否会运行您在加载的页面中找到的JavaScript方法名称 loadHTMLString ,有点含糊不清,文档中没有太多说明)。

更多信息:




Is it possible to read the raw HTML content of a web page that has been loaded into a UIWebView?

If not, is there another way to pull raw HTML content from a web page in the iPhone SDK (such as an equivalent of the .NET WebClient::openRead)?

解决方案

The second question is actually easier to answer. Look at the stringWithContentsOfURL:encoding:error: method of NSString - it lets you pass in a URL as an instance of NSURL (which can easily be instantiated from NSString) and returns a string with the complete contents of the page at that URL. For example:

NSString *googleString = @"http://www.google.com";
NSURL *googleURL = [NSURL URLWithString:googleString];
NSError *error;
NSString *googlePage = [NSString stringWithContentsOfURL:googleURL 
                                                encoding:NSASCIIStringEncoding
                                                   error:&error];

After running this code, googlePage will contain the HTML for www.google.com, and error will contain any errors encountered in the fetch. (You should check the contents of error after the fetch.)

Going the other way (from a UIWebView) is a bit trickier, but is basically the same concept. You'll have to pull the request from the view, then do the fetch as before:

NSURL *requestURL = [[yourWebView request] URL];
NSError *error;
NSString *page = [NSString stringWithContentsOfURL:requestURL 
                                          encoding:NSASCIIStringEncoding
                                             error:&error];

EDIT: Both these methods take a performance hit, however, since they do the request twice. You can get around this by grabbing the content from a currently-loaded UIWebView using its stringByEvaluatingJavascriptFromString: method, as such:

NSString *html = [yourWebView stringByEvaluatingJavaScriptFromString: 
                                         @"document.body.innerHTML"];

This will grab the current HTML contents of the view using the Document Object Model, parse the JavaScript, then give it to you as an NSString* of HTML.

Another way is to do your request programmatically first, then load the UIWebView from what you requested. Let's say you take the second example above, where you have NSString *page as the result of a call to stringWithContentsOfURL:encoding:error:. You can then push that string into the web view using loadHTMLString:baseURL:, assuming you also held on to the NSURL you requested:

[yourWebView loadHTMLString:page baseURL:requestURL];

I'm not sure, however, if this will run JavaScript found in the page you load (the method name, loadHTMLString, is somewhat ambiguous, and the docs don't say much about it).

For more info:

这篇关于从UIWebView中读取HTML内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆