我需要在 ios 中在一个简单的 webview 中创建一个应用程序。
我用example https://github.com/vandadnp/iOS-8-Swift-Programming-Cookbook/blob/master/chapter-basics/Loading%20Web%20Pages%20with%20WebKit/Loading%20Web%20Pages%20with%20WebKit/ViewController.swift最近升级了ios 8.4.1。升级后,应用程序停止工作,显示黑屏。
import UIKit
import WebKit
class ViewController: UIViewController, WKNavigationDelegate {
var webView: WKWebView?
/* Start the network activity indicator when the web view is loading */
func webView(webView: WKWebView,
didStartProvisionalNavigation navigation: WKNavigation){
UIApplication.sharedApplication().networkActivityIndicatorVisible = true
}
/* Stop the network activity indicator when the loading finishes */
func webView(webView: WKWebView,
didFinishNavigation navigation: WKNavigation){
UIApplication.sharedApplication().networkActivityIndicatorVisible = false
}
func webView(webView: WKWebView,
decidePolicyForNavigationResponse navigationResponse: WKNavigationResponse,
decisionHandler: ((WKNavigationResponsePolicy) -> Void)){
print(navigationResponse.response.MIMEType)
decisionHandler(.Allow)
}
override func viewDidLoad() {
super.viewDidLoad()
/* Create our preferences on how the web page should be loaded */
let preferences = WKPreferences()
preferences.javaScriptEnabled = false
/* Create a configuration for our preferences */
let configuration = WKWebViewConfiguration()
configuration.preferences = preferences
/* Now instantiate the web view */
webView = WKWebView(frame: view.bounds, configuration: configuration)
if let theWebView = webView{
/* Load a web page into our web view */
let url = NSURL(string: "http://www.apple.com")
let urlRequest = NSURLRequest(URL: url!)
theWebView.loadRequest(urlRequest)
theWebView.navigationDelegate = self
view.addSubview(theWebView)
}
}
}
如果有示例代码吗?
谢谢。
我的应用程序中也发生了类似的问题。这就是我在 Objective-C 中解决这个问题的方法:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
NSLog(@"request.URL.scheme: %@", request.URL.scheme);
BOOL calledForReload = NO;
if ([[request.URL.scheme lowercaseString] isEqualToString:@"about"]) {
//have we used reload yet?
if (!calledForReload) {
calledForReload = YES;
[webView reload];
} else {
// other response
// decide what you wish to do if you get another about:blank
}
}
return YES;
}
基本上我看到的是iOS 8.4.1UIWebView
突然打电话about:blank
作为第一个 URL。我的解决方案是检查about:blank
并询问UIWebView
仅在第一次时重新加载。
答案不在 SWIFT 中,但希望解决方案逻辑可以轻松转换为您的代码。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)