0
我有一個UIWebView
,我使它加載一個HTML字符串。該字符串包含圖像的http url。圖像被加載,但不調用shouldStartLoadWithRequest:
方法。它只被調用一次,對於about : blank
。但是,所有圖像請求都成功傳遞到我的NSURLCache
子類中,轉換爲cachedResponseForRequest:
方法。但我想處理shouldStartLoadWithRequest:
方法中的請求。可以做任何事情嗎?我怎樣才能讓這些請求進入委託方法?shouldStartLoadWithRequest:不需要圖像
下面是代碼:
@interface URLCache : NSURLCache
@end
@implementation URLCache
- (NSCachedURLResponse *)cachedResponseForRequest:(NSURLRequest *)request
{
NSLog(@"REQUEST = %@", request.URL);
return [super cachedResponseForRequest:request];
}
@end
@interface ViewController()<UIWebViewDelegate>
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[NSURLCache setSharedURLCache:[URLCache new]];
NSString* path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"html"];
NSString* html = [NSString stringWithContentsOfFile:path
encoding:NSUTF8StringEncoding
error:nil];
self.webView.delegate = self;
[self.webView loadHTMLString:html baseURL:nil];
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSLog(@"URL = %@", request.URL);
return YES;
}
@end