WkWebView不会从原始IP地址加载图像

时间:2015-10-15 16:51:15

标签: image wkwebview

如果我使用WKWebView(loadHTMLString)加载本地html,其中包含指向当前子网(192.168.178.30)内原始IP地址的图像链接,则图像不会显示。它使用需要解析的地址(例如“localhost”)。发生在iOS 8和iOS 9下.NSAppTransportSecurity设置为NSAllowsArbitraryLoads。

有什么问题?有什么方法可以绕过它吗? 问候, 马库斯

1 个答案:

答案 0 :(得分:0)

我发现加载图片失败了,因为它需要站点的凭据(登录)。所以我的第一个猜测是错误的(它与原始IP地址有关)。

我添加了WKNavigationDelegate的选择器:

- (void)webView:(WKWebView *)webView didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(NSURLSessionAuthChallengeDisposition, NSURLCredential *))completionHandler
{
    NSURLCredential* credential = [[NSURLCredential alloc] initWithUser:<username> password:<password> persistence:NSURLCredentialPersistenceNone];
    completionHandler(NSURLSessionAuthChallengeUseCredential, self.credential);
}

它有效。我认为还有其他方法可以做到这一点,这个方法足以达到我的目的。