Search code examples
htmliosswiftbundle

Write to local html file in Swift


I have some html code that I have receive from json. Now I have to write this html code to html document that is in my project and than show it in webView. How can I write my HTML code to my html file in my project.

Here is in my content variable I have HTML code.

func getArticles() {
    //let id = "1018"
    let url = "http://www.salemkz.kz/inc/API/getArticles.php?idArticle=170"
    print(url)
    URLSession.shared.dataTask(with: URL(string: url)!) { (data, response, error) in
        if (error != nil) {
            print(error!.localizedDescription)
        }
        else {
            let jsons = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
            if let results = jsons["result"] as? [[String: AnyObject]] {
                //print("got Result", results)
                for result in results {
                    let content = result["content"] as? String
                    print(content!)
                }

            }

        }
        }.resume()

}

And I have to write this code to this html file (named "articles-1.html") : enter image description here

How to do this ???


Solution

  • Well, you can generate this document manually. Not an elegant solution, but will work. Try this:

    @IBOutlet weak var webView: UIWebView!
    
    var generatedHtml : String?
    var receivedContent: String? = ""
    
    func getArticles() {
            //let id = "1018"
            let url = "http://www.salemkz.kz/inc/API/getArticles.php?idArticle=170"
    
            print(url)
            URLSession.shared.dataTask(with: URL(string: url)!) { [weak self]
                (data, response, error) in
                if (error != nil) {
                    print(error!.localizedDescription)
                }
                else {
                    let jsons = try! JSONSerialization.jsonObject(with: data!, options: .allowFragments) as! [String: AnyObject]
                    if let results = jsons["result"] as? [[String: AnyObject]] {
                        //print("got Result", results)
                        for result in results {
                            let content = result["content"] as? String
                            if (content != nil) {
                                self?.receivedContent?.append(content!)
                            }
                            print(content!)
                        }
                        self?.generatedHtml = self?.generateString()
                        if ((self?.generatedHtml) != nil) {
                            self?.webView.loadHTMLString((self?.generatedHtml!)!, baseURL: nil)
                        }
                    }
    
                }
                }.resume()
        }
    
    func generateString() -> String? {
            var resultString: String?
    
            let firstString = "<DOCTYPE HTML> \r <html lang=\"en\" \r <head> \r <meta charset = \"utf-8\"> \r </head> \r <body>"
            let endString = "</body> \r </html>";
            resultString = firstString + self.receivedContent! + endString;
    
            return resultString
        }
    

    webView here is a view, where you want to display this html. If you want to save the html to file, you can also do this, just modify code a bit. (Use String. writeToFile method)