ios 8.4.1 webview black screen

╄→гoц情女王★ 提交于 2019-12-21 21:51:21

问题


I need to make an application in ios in a simple webview. I use example recently upgraded ios 8.4.1. After the upgrade, the application stops working, displays a black screen.

    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)

    }

  }

}

If any example code?

Thank you.


回答1:


I had a similar issue happen in our app. This is how I solved it in 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;
}

Basically I saw iOS 8.4.1 UIWebView suddenly calls about:blank as the first URL. My solution was to check for about:blank and ask the UIWebView to reload only the first time.

The answer is not in SWIFT, but hopefully the solution logic is easily translated to your code.




回答2:


Ran into this when Google Translate was triggering my WKWebView to render a blank page after executing the callback. Here is a iOS 10, swift version of the same thing as @dredful - which seems to cover up the issue I am seeing:

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


来源:https://stackoverflow.com/questions/32408352/ios-8-4-1-webview-black-screen

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!