Wkwebview Not Loading Local Files Under iOS 8

Cannot load a local html file into a WKWebView on device only (works on simulator)

OK I have found "why" it was not working. I changed this:

[self.webView loadFileURL:URL allowingReadAccessToURL:URL];

to this:

[self.webView loadFileURL:URL.absoluteURL allowingReadAccessToURL:rootAppURL];

with rootAppURL being:

NSString *rootappPath = [NSFileUtility pathRelativeToContentDirectoryForSubpath:@"/www"];
NSURL *rootAppURL = [NSURL fileURLWithPath:rootappPath isDirectory:YES];

And it now works on ios < 13

iOS: WKWebView does not load local JS files immediately

WKWebView initialization and first load are slow. It is better you initialize and load WKWebView before the view controller containing your WKWebView is loaded. You can use below project to check how they have achieved this for your reference and also they have done comparison of time taken to load.

https://github.com/bernikovich/WebViewWarmUper

Also hope that loading an animation is not taking much time.

Swift WKWebView Loading local file not working on a device

Thank you for any one who tried to answer my question. I have released that this is an error with the WebKit lib that Apple are trying to fix. However I have found a good workaround that required little work.

I open the local file and read its content and then send that string into a webView.loadHTMLString method that compiles the hmtl that was in the file. That way you avoid the issues with iOS not being able to find the path to the local file.

Here is an example of reading a file and then opening it for any one who has the same issues:

    let path2 = NSBundle.mainBundle().pathForResource("index", ofType: "html")
var text = String(contentsOfFile: path2!, encoding: NSUTF8StringEncoding, error: nil)!

webView!.loadHTMLString(text, baseURL: url)

Kind regards,
Dimitar

WKWebView cannot load local resource in HTML

Add all the HTML directories & files in your project.

let webView = WKWebView()
if let htmlPath = Bundle.main.path(forResource: "directory/index", ofType: "html") {
let htmlUrl = URL(fileURLWithPath: htmlPath, isDirectory: false)
webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl)
view = webView
}

Sample Image

WKWebView not loading slider images

Add following key into info.plist if not added

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>


Related Topics



Leave a reply



Submit