Ios WKWebview赢得';无法正确加载某些网站
我需要将各种各样的网站URL(来自Web服务)加载到WKWebview中 有些网站表现良好,只需将URL中的“http”替换为“https”,即可正常加载:Ios WKWebview赢得';无法正确加载某些网站,ios,objective-c,https,wkwebview,app-transport-security,Ios,Objective C,Https,Wkwebview,App Transport Security,我需要将各种各样的网站URL(来自Web服务)加载到WKWebview中 有些网站表现良好,只需将URL中的“http”替换为“https”,即可正常加载: http://www.yahoo.com/news -> loads blank **https://www.yahoo.com/news -> loads OK!!** URLs without the https:// prefix will NOT load at all. http://www.google.com
http://www.yahoo.com/news -> loads blank
**https://www.yahoo.com/news -> loads OK!!**
URLs without the https:// prefix will NOT load at all.
http://www.google.com -> blank
**https://www.google.com -> Loads OK!!!**
http://www.bbc.co.uk/news (no .plist Exception) -> blank
https://www.bbc.co.uk/news (no .plist Exception) -> blank
http://www.bbc.co.uk/news (WITH .plist Exception) -> blank
https://www.bbc.co.uk/news (WITH .plist Exception) -> blank
(我粘贴在MAC OS浏览器上,并被重定向到,所以我也尝试了:)
其他URL的行为与上面的BBC或Yahoo示例类似
这是我在.plist文件中使用的那种异常
这是我的代码:
- (void)viewDidLoad{
[super viewDidLoad];
CGRect wkFrame = _wios10container.frame;
NSLog(@"WPVC - viewDidLoad, wkFrame: %@",NSStringFromCGRect(wkFrame));
WKWebViewConfiguration *wkConfiguration = [[WKWebViewConfiguration alloc] init];
_webviewIOS10 = [[WKWebView alloc] initWithFrame:_wios10container.frame configuration:wkConfiguration];
_webviewIOS10.navigationDelegate = self;
[_wios10container addSubview:_webviewIOS10];
}
我的问题是:
一种更可靠的方法是使用WKNavigationDelegate,并在您的例子中计算URL。为了处理一个巨大的列表(没有更多关于该列表的知识),我将基于它执行正则表达式计算。还可以根据需要创建重定向策略 例如:
class WebNavigationHandler: NSObject, WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
//Based on the navigation action decide what to do.
if canINavigate {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Swift.Void) {
// Base on the navigationResponse
if canINavigate {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
}
}
}
let webview = WKWebView()
webview.navigationDelegate = WebNavigationHandler()
希望它能回答您的问题。我想您正在寻找
NSAppTransportSecurity
的NSAllowArbitraryLoadsInWebContent
设置。请参阅此苹果文档:经过多次尝试和错误,这是对我有效的公式:
我会试试的,谢谢。我指的是一个大列表:我不能通过在.plist文件中单独设置来预测运行时可能出现的所有URL。当我尝试你的方法时,我会回来的。
class WebNavigationHandler: NSObject, WKNavigationDelegate {
func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Swift.Void) {
//Based on the navigation action decide what to do.
if canINavigate {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
}
}
func webView(_ webView: WKWebView, decidePolicyFor navigationResponse: WKNavigationResponse, decisionHandler: @escaping (WKNavigationResponsePolicy) -> Swift.Void) {
// Base on the navigationResponse
if canINavigate {
decisionHandler(.allow)
} else {
decisionHandler(.cancel)
}
}
}
let webview = WKWebView()
webview.navigationDelegate = WebNavigationHandler()
<key>bbc.co.uk</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
<false/>
<key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
<true/>
<key>NSThirdPartyExceptionMinimumTLSVersion</key>
<string>TLSv1.2</string>
<key>NSRequiresCertificateTransparency</key>
<false/>
</dict>
NSURL *wkURL = [NSURL URLWithString:wkURLstring];
SFSafariViewController *safariVC = [[SFSafariViewController alloc] initWithURL:wkURL];
[self.view.window.rootViewController presentViewController:safariVC animated:YES completion:^{
NSLog(@"Full Safari Webview Launched");
}];