Ibad Ur Rahman Ibad Ur Rahman - 1 year ago 97
HTML Question

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

Answer Source

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

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

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download