您如何检测在WebView中显示包含非SSL资产的SSL URL的任何尝试?就像浏览器为您提供混合SSL警告一样。
我在UIWebView或UIWebViewDelegate上看不到任何明显的属性。我可以继承NSURLProtocol,并以某种方式将非SSL连接传递回UIWebView。有更简单的方法吗?
答案 0 :(得分:1)
一种解决方案是创建自定义URL缓存并将其设置为默认值,以便捕获所有HTTP请求,然后检查混合SSL内容。
以下是一个例子:
@interface MonitoringURLCache : NSURLCache
@end
@implementation MonitoringURLCache
- (NSCachedURLResponse*)cachedResponseForRequest:(NSURLRequest *)request {
NSURL *requestURL = [request URL];
NSURL *pageURL = [request mainDocumentURL];
if ([[pageURL scheme] isEqualToString:@"https"] && [[requestURL scheme] isEqualToString:@"http"]) {
NSLog(@"Non safe resource: %@ referenced from page: %@", requestURL, pageURL);
}
return [super cachedResponseForRequest:request];
}
@end
/// Register your custom cache
MonitoringURLCache *cache = [[MonitoringURLCache alloc] init];
[NSURLCache setSharedURLCache:cache];
/// Make a request to a website with mixed SSL content
NSURL *url = [NSURL URLWithString:@"https://some-website-with-mixed-ssl-content/"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webview loadRequest:request];