【发布时间】:2017-06-21 10:20:16
【问题描述】:
我有一个UIWebView,我让它加载一个 HTML 字符串。该字符串包含图像的 http url。图像被加载,但没有为它们调用 shouldStartLoadWithRequest: 方法。对于about : blank,它只被调用一次。然而,所有图像请求都成功传递到我的NSURLCache 子类,传递到cachedResponseForRequest: 方法。但我想处理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
【问题讨论】:
标签: ios objective-c uiwebview nsurlrequest nsurlcache