2

I am trying to load html page in WKWebView which is linked to external javascript and css files. My web view shows me the html page but it does not seem like that its respecting css file values. Can anyone please suggest me as to what am I doing wrong here. Following my code to add html page in WKWebView:

  override func viewDidLoad() {
    super.viewDidLoad()

    let path = NSBundle.mainBundle().pathForResource("index", ofType: "html");
    let htmlString = String.stringWithContentsOfFile(path, encoding: NSUTF8StringEncoding, error: nil);

    self.webView.loadHTMLString(htmlString, baseURL: NSURL.fileURLWithPath(NSBundle.mainBundle().bundlePath));

  }

Also, here is the project structure where .css, .js and .html files re added.

enter image description here

Any help would be appreciated.

2
  • 1
    This seems to be a bug in Beta 4. I have the same issue but so far my research has not turned up a solution. Hope the next beta resolves the many wkwebview issues floating around. Commented Jul 30, 2014 at 23:29
  • Check the answer in this link. It has a better explanation: stackoverflow.com/questions/37922628/… Commented Dec 17, 2019 at 14:34

1 Answer 1

0

The problem is that the WKWebView renders page content inside a sandbox process that cannot access files in your application bundle. This effectively means that file:// URLs do not work.

I ended up including a small web server in my application to make it easier to serve content to the WKWebView.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.