简体   繁体   中英

ios 8.4.1 webview black screen

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.

截图

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.

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM