WKWebView hangs when loading local web page with certain network setting.

I have a WKWebView that loads a local set of web pages using WKWebViewConfiguration

to set the parameter @"allowFileAccessFromFileURLs"

to true.

The request is configured something like this:

NSURL *url = [[NSBundle mainBundle] URLForResource:@"testPage" withExtension:@"html" subdirectory:@"html/pages"];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10.0f];

      

The request is then sent using the following WKWebview method:

- (nullable WKNavigation *)loadRequest:(NSURLRequest *)request;

      

My problem is the device has a current connection, but no network traffic. The webview as an on-screen item is added to the screen and the request is executed, but the webview will show a white screen for about 50 seconds before displaying the local content.

Everything in webview downloads regardless of network state as it downloads locally when there is no set of links configured.

For example, if the device is connected to a wireless network, but the air conditioner of the network line is set to 100% loss. A webview is created and a request is sent to download local content, causing the load to hang.

I got the idea that the WKWebView might try to do some kind of validation in the background that requires a network transaction, but I did some network profiling with the tools as well as some timeline recording in the Safari web browser and I couldn't do anything see what will hang.

The only reason I can think of loading local content in 50 seconds or so is because it hit some kind of WKWebView timeout to load the network connection.

Any help would be greatly appreciated, thanks.

+3


source to share


1 answer


Ok, so for anyone else who stumbles over this, I've found what I am doing wrong.

The problem was not actually WKWebview or the web content itself, as I was handling the web browser loading completion.

in method:

- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation{

      



I was listening to event completion evaluating some JS like so:

-(void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation
{

    [webView evaluateJavaScript:@"document.body.innerHTML" completionHandler:^(id result, NSError *error) 
    {
        if (result != nil) {
        [self doCertainNetworkEvent]; //here another method is called with a networking function inside of it. 
    }
        if(error) 
        {
            NSLog(@"evaluateJavaScript error : %@", error.localizedDescription);
        }
    }];

}

      

The completion block, of course, couldn't finish until the network function in the didFinishNavigation method call was finished (which couldn't be due to the lack of traffic.)

0


source







All Articles