how to load local html file to string variable in ios swift?

16,149

Solution 1

Copy the html into your project directory and use this code :

@IBOutlet weak var webView: UIWebView!
override func viewDidLoad() {
    super.viewDidLoad()

        var htmlFile = NSBundle.mainBundle().pathForResource("MyHtmlFile", ofType: "html")
        var htmlString = try? String(contentsOfFile: htmlFile!, encoding: NSUTF8StringEncoding)
        webView.loadHTMLString(htmlString!, baseURL: nil)


}

Solution 2

In Swift 3:

    let htmlFile = Bundle.main.path(forResource:"MyHtmlFile", ofType: "html")
    let htmlString = try? String(contentsOfFile: htmlFile!, encoding: String.Encoding.utf8)
    webView.loadHTMLString(htmlString!, baseURL: nil)

I recommend using optional chaining instead of force unwrapping htmlString as above.

Solution 3

You can write some utility method like this and get the html string and load it to webview. I used the URL approach here

   private func getHTML() -> String {
        var html = ""
        if let htmlPathURL = Bundle.main.url(forResource: "test", withExtension: "html"){
            do {
                html = try String(contentsOf: htmlPathURL, encoding: .utf8)
            } catch  {
                print("Unable to get the file.")
            }
        }

        return html
    }
Share:
16,149
ibad ur rahman
Author by

ibad ur rahman

Senior Software Engineer At Satsuma Droid. Work in iOs, Android, Windows phone and wordpress.

Updated on July 22, 2022

Comments

  • ibad ur rahman
    ibad ur rahman almost 2 years

    i want to load my local html file into a string variable. i don't know how to do it. please help me. i found below link but it load it from online url. Swift & UIWebView element to hide

  • Tomas Javurek
    Tomas Javurek over 5 years
    I'm quite confused. Where is the exact path defined? I'm a newbie in swift and also in xcode:/ However I cannot figure out... maybe: How to load the file in xcode project to target it this way?
  • Rizwan Shaikh
    Rizwan Shaikh over 5 years
    @ Tomas Javurek just drag your file inside the project folder
  • Tomas Javurek
    Tomas Javurek over 5 years
    @RizwanShaikh I did it this way, intuitively. I already have 'default.html' in my xcode project and I try to load it this way let htmlFile = Bundle.main.path(forResource: "default", ofType: "html"). The file is also a part of compiled bundle and is in Resources folder. But while I try to get the content of file, it produce an error. I tried also print the value of htmlFile, unwrapped, and it returns nil..., so my problem is somewhere else... any idea what I am doing wrong?
  • Tomas Javurek
    Tomas Javurek over 5 years
    I figure it out by using this code: let bundle = Bundle(for: MyView.self) let url = bundle.url(forResource:"default", withExtension:"html") let request = URLRequest(url: url!) self.webView.load(request)
  • ScottyBlades
    ScottyBlades about 3 years
    @TomasJavurek, you are my hero. You should add that as an answer.