admin管理员组文章数量:1410674
I have a website that I wish to load in a UIWebView, but it is full of images and takes ages to load. The images are useless, and only serve to reduce the usability on the iPhone. I dont own the website so I cannot change the site's actual code.
The webpage is heavily linked in to the web with ASP.NET and AJAX (needs external files), so i dont think it is possible to have it load an HTML string.
I want to stop the images from loading altogether. So how do i block them? Change the HTML code as its loading somehow, or block images from being loaded?
I have a website that I wish to load in a UIWebView, but it is full of images and takes ages to load. The images are useless, and only serve to reduce the usability on the iPhone. I dont own the website so I cannot change the site's actual code.
The webpage is heavily linked in to the web with ASP.NET and AJAX (needs external files), so i dont think it is possible to have it load an HTML string.
I want to stop the images from loading altogether. So how do i block them? Change the HTML code as its loading somehow, or block images from being loaded?
Share Improve this question asked Apr 8, 2010 at 9:40 Zac AltmanZac Altman 1,2134 gold badges25 silver badges37 bronze badges4 Answers
Reset to default 2I have 3 thoughts.
- Use a proxy to filter the HTML
- Filter the HTML yourself by downloading it first
- Just download the html and load it using a file:// URL. This will most likely prevent the image references from being resolved - though it will leave ugly squares everywhere
The UIWebView delegate approach does not work, you're right ! new answer:
Right, you need to go one level deeper to catch the NSURLRequest
ing from the UIWebView
. The blog you're referring to makes use of the NSURLCache
and I think this is a good start point:
Did you try to subclass the NSURLCache, and then override the -(NSURLCacheResponse*) cachedResponseForRequest:(NSURLRequest*) request
If you want to avoid the request being performed, you need to return something (as an NSURLCachedResponse
). You can for instance, get an image that is statically defined in your app (maybe an PNG with size 0,0 ).
If you return nil
, the request will be performed.
I'm using this approach to force the UIWebView
to get populated with a local cache. See my detailled answer about this here :
How to save the content in UIWebView for faster loading on next launch?
Wrong first answer :/
Add yourself as delegate to the UIWebView
(UIWebViewDelegate
).
For each image that the UIWebView
attempts to load, the delagate gets the following callback invoked :
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
Just check if the request is related to an image you want to avoid to download. In such a case, just return NO
to this callback : the image won't be loaded in the UIWebView
.
By using this approach, no change is required on server side.
I would suggest another option to try:
using webView:shouldStartLoadWithRequest:navigationType:
of the WebView's delegate to return NO when it try to load image.
You can use the following line of code, whenever you want to stop the webView loading:
[webView stopLoading];
本文标签: javascriptStop Images from loading in UIWebViewStack Overflow
版权声明:本文标题:javascript - Stop Images from loading in UIWebView - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744994019a2636559.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论