我需要将各种各样的网站URL(来自网络服务)加载到WKWebview中。
有些网站表现得非常好,只需在网址中替换“https”中的“http”即可正确加载:
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,或上面的雅虎例子。
这是我在.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];
}
我的问题是:
答案 0 :(得分:0)
更强大的方法是使用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()
希望它能回答你的问题。
答案 1 :(得分:0)
我认为您正在寻找NSAllowArbitraryLoadsInWebContent
的{{1}}设置。请参阅此Apple文档:https://developer.apple.com/library/content/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html
答案 2 :(得分:0)
经过多次反复试验,这是适合我的公式:
<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>
可选,前者也适用于其他方法,在SFSafariViewController上加载URL。我将采用这种方法,这种方法非常可靠,甚至可以在内置导航中使用后退,下一步,刷新和共享按钮。酒吧。这是代码:
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");
}];