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

0

5 Answers 5

20

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)


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

5 Comments

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 just drag your file inside the project folder
@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?
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)
@TomasJavurek, you are my hero. You should add that as an answer.
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.

Comments

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
    }

Comments

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)

Comments

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)
            }
        }

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.