ios 8.4.1 webview黑屏

时间:2015-09-05 00:35:05

标签: ios webview ios8

我需要在一个简单的webview中在ios中创建一个应用程序。 我使用example最近升级的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)

    }

  }

}

如果有任何示例代码?

谢谢。

screenshot

2 个答案:

答案 0 :(得分:3)

我的应用程序中发生了类似的问题。这就是我在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.1 UIWebView突然调用about:blank作为第一个URL。我的解决方案是检查about:blank并要求UIWebView仅在第一次重新加载。

答案不在SWIFT中,但希望解决方案逻辑可以轻松转换为您的代码。

答案 1 :(得分:1)

当谷歌翻译在执行回调后触发我的WKWebView呈现空白页时,请进入此状态。这是一个iOS 10,与@dredful相同的快速版本 - 这似乎掩盖了我所看到的问题:

func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
    if "about" == navigationAction.request.url?.scheme {
        decisionHandler(.cancel);
        webView.reload()
    }
}