10

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

Community
  • 1
  • 1
ibad ur rahman
  • 1,381
  • 4
  • 18
  • 40

5 Answers5

18

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)


}
Rizwan Shaikh
  • 2,824
  • 2
  • 27
  • 49
  • 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? – Tomas Javurek Oct 28 '18 at 21:40
  • @ Tomas Javurek just drag your file inside the project folder – Rizwan Shaikh Oct 29 '18 at 13:18
  • 1
    @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 Oct 30 '18 at 14:24
  • 1
    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)` – Tomas Javurek Oct 31 '18 at 20:49
  • @TomasJavurek, you are my hero. You should add that as an answer. – ScottyBlades Mar 07 '21 at 09:29
6

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.

feco
  • 4,384
  • 5
  • 27
  • 44
askielboe
  • 873
  • 13
  • 14
1

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
    }
anoop4real
  • 7,598
  • 4
  • 53
  • 56
0

Swift 3 syntax:

    guard
        let file = Bundle.main.path(forResource: "agreement", ofType: "html"), 
        let html = try? String(contentsOfFile: file, encoding: String.Encoding.utf8)
    else {
        return
    }
    webView.loadHTMLString(html, baseURL: nil)
superarts.org
  • 7,009
  • 1
  • 58
  • 44
0

Simple answer

 let indexPath = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "/")
        if let indexPath = indexPath
        {
            do
            {
                let htmlContent = try String(contentsOfFile: indexPath, encoding: String.Encoding.utf8)

                let base = Bundle.main.resourceURL 
                self.webView.loadHTMLString(htmlContent, baseURL: base)

            }
            catch let err as NSError
            {
                print(err.debugDescription)
            }
        }
Faizul Karim
  • 146
  • 13