我需要将各种各样的网站 URL(来自网络服务)加载到 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 -> 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 浏览器上粘贴了 https://www.bbc.co.uk/news,并被重定向到 https://www.bbc.co.uk/news,所以我也尝试了
https://www.bbc.com/news (WITH .plist Exception) -> Images and text are loaded in a messy, unformatted, basic way.
http://www.bbc.com/news (WITH .plist Exception) -> Images and text are loaded in a messy, unformatted, basic way.
https://www.bbc.com/news (WITHOUT .plist Exception) -> blank
http://www.bbc.com/news (WITHOUT .plist Exception) -> blank
其他网址的行为与 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()
希望它能回答你的问题。
关于ios - WKWebview 无法正确加载某些网站,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42671372/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |