Search code examples
htmliosstringswiftuiwebview

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


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


Solution

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