开发者

How to restrict UIWebview to download images?

开发者 https://www.devze.com 2023-03-01 16:36 出处:网络
How can i 开发者_JS百科restrict UIWebView not to download any type of images? Regards, JohnI can think of two ways right now:

How can i 开发者_JS百科restrict UIWebView not to download any type of images?

Regards, John


I can think of two ways right now:

1. NSURLProtocol subclass

Subclass and register NSURLProtocol to intercept http requests, and for urls ending in known image extensions just don't make the request, just return an empty string as body. This is error prone as for example:

<img src="http://foobar.com/deliverImage.php?id=400&size=lager" />

is valid to request image resources and the URL doesn't hint anything. You could of course go one step further and download, or at least start the request and intercept the headers received and check for the mime type returned or even the file's body.

2. Parse and filter html before rendering

The second solution is a bit easier: Download the html not directly in the webview, but instead use NSURLConnection or ASIHTTPRequest to get the page. Now you can parse it and replace the content of the src attribute of all img tags.

Still this won't prevent images being loaded with css for backgrounds for example.

0

精彩评论

暂无评论...
验证码 换一张
取 消