问题
I want to use UIWebView to load url, but don't load the image in the webpage.I want UIWebView just display the plain text in the webpage.
thanks.
回答1:
Your best bet is to set a delegate for the webView and implement the delegate method:
- webView:shouldStartLoadWithRequest:navigationType:
Depending on how the page is constructed, you could get a notification for when the image wants to load, and return NO to stop the webView from loading that resource.
Edit:
If you didn't set your web view's delegate in the xib, then at some point in setup:
self.webview.delegate = self;
Then, in the class you set as the delegate, something like:
-(BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSRange rangeOfPNG = [request.URL.absoluteString rangeOfString:@".png"];
NSRange rangeOfJPG = [request.URL.absoluteString rangeOfString:@".jpg"];
if (rangeOfPNG.location != NSNotFound || rangeOfJPG.location != NSNotFound)
{
return (NO); // Tells the webview to skip loading this part of the page
}
return (YES); // Allows the webview to load everything else
}
So anything with an extension of .png or .jpg gets skipped.
This is highly dependent on the structure of the page you're loading. For more complex pages, there might not be a callback for a specific image.
It helps greatly if you know the structure of the page you're attempting to load.
You can also use Safari to examine the code for the page to determine what's in there. Also, setting a breakpoint at the start of the shouldStartLoadWithRequest delegate method will let you see exactly what requests come through, and you might be able to use that to hardcode strings that you want the webview to skip loading.
来源:https://stackoverflow.com/questions/32516018/how-to-block-load-image-in-uiwebview-ios