1

I have a WKWebView that works perfectly fine when given a URL or a URLRequest, but when I am trying to load local bundle html files into it, it just doesn't display anything.

I am trying it like so:

let webView = WKWebView() if let bundleURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "mySubDirectory/v1") { webView.loadFileURL(bundleURL, allowingReadAccessTo: bundleURL.deletingLastPathComponent()) } 

I have tested that the bundleURL actually returns the right path and tried copying + pasting it in my browser at run time and it works great: file:///Users/myuser/Library/Developer/Xcode/DerivedData/FireApp-gugmufgdkejtfdhglixebzhokhfe/Build/Products/Debug-iphonesimulator/SteppingIntoFireFramwork.framework/mySubDirectory/v1/index.html

What could be the problem?

0

4 Answers 4

2

If the URL works, then it seems that your webview is having trouble loading it.

I once had this issue... Check that you don’t have only “http” & “https” enabled for the allowed URL scheme

Sign up to request clarification or add additional context in comments.

3 Comments

OMFG, someone actually enabled http and https only for the webview to load! Thanks a lot! I would not have found it otherwise :O
Where is that option? I couldn't find it.
@Gerard I can not find it either. It used to be under Info.pList.
0
import UIKit import WebKit class ViewController: UIViewController, WKNavigationDelegate { override func viewDidLoad() { super.viewDidLoad() let webView = WKWebView() let htmlPath = Bundle.main.path(forResource: "index", ofType: "html") let htmlUrl = URL(fileURLWithPath: htmlPath!, isDirectory: false) webView.loadFileURL(htmlUrl, allowingReadAccessTo: htmlUrl) webView.navigationDelegate = self view = webView } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() } } 

Full solution here

Comments

0

Your web view is just a local temporary variable. It has zero size and is not in the interface, so you never see anything happen.

Comments

0

Try to remove deletingLastPathComponent() from your example. Also you can try to use load(request) instead of loadFileURL().

if let bundleURL = Bundle.main.url(forResource: "index", withExtension: "html", subdirectory: "mySubDirectory/v1") { let request = URLRequest(url: bundleURL) webView.load(request) } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.