Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading local css & js files into WKWebView

in Swift 2.1.1 & Xcode 7.1
My code uses WKWebView and loads index.html form a local file but fails to load index.css and other javascript files as shown in the head tag.

My best guess is that the baseURL is not correct, if so, How can I set the baseURL correctly? Thanks

import UIKit
import WebKit

class ViewController: UIViewController {
@IBOutlet var containerView: UIView! = nil  //allows the class to refrence WKWebView
var webView: WKWebView?

override func loadView() {
    super.loadView()

    self.webView = WKWebView()
    self.view = self.webView!
}

override func viewDidLoad() {
    super.viewDidLoad()

    let HTMLDocumentPath = NSBundle.mainBundle().pathForResource("index", ofType: "html")
    let HTMLString: NSString?

    do {
        HTMLString = try NSString(contentsOfFile: HTMLDocumentPath!, encoding: NSUTF8StringEncoding)
        let baseUrl  = NSURL.fileURLWithPath("")
        webView!.loadHTMLString(HTMLString as! String, baseURL: baseUrl)

    } catch {
        HTMLString = nil
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
}

}

   <head>
      <meta charset="UTF-8">
      <title>RRR</title>
      <link rel="stylesheet" href="jquery.mobile-1.4.5.css"/>
      <link rel="stylesheet" href="index.css"/>
      <script src="jquery-1.11.3.js"></script>
      <script src="jquery.mobile-1.4.5.js"></script>
      <meta name="viewport" content="width=device-width"/>
   </head>

enter image description here

like image 643
Fred J. Avatar asked Jan 04 '16 18:01

Fred J.


People also ask

How do I load a CSS file?

CSS can be added to HTML documents in 3 ways: Inline - by using the style attribute inside HTML elements. Internal - by using a <style> element in the <head> section. External - by using a <link> element to link to an external CSS file.

Why is my CSS not applied?

This usually happens when the browser serves a cached version of your CSS file. To invalidate the cache and serve the latest version of your CSS file, you can perform a hard reload on the browser.


1 Answers

After a bit of reading here about the file url I was able to solve the problem.

Here is the code

import UIKit
import WebKit

class ViewController: UIViewController {
    @IBOutlet var containerView: UIView! = nil  //allows the class to reference WKWebView
    var webView: WKWebView?

    override func loadView() {
        super.loadView()

        self.webView = WKWebView()
        self.view = self.webView!
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        let baseUrl = NSURL(string: "file:///<yourFilePath>/abc/")
        let path = NSBundle.mainBundle().pathForResource("abc/index", ofType: "html")
        let HTMLString: NSString?

        do {
            HTMLString = try NSString(contentsOfFile: path!, encoding: NSUTF8StringEncoding)
            webView!.loadHTMLString(HTMLString as! String, baseURL: baseUrl )

        } catch {
            HTMLString = nil
        }
    }
}
like image 143
Fred J. Avatar answered Oct 29 '22 13:10

Fred J.