iOS download and save HTML file

前端 未结 4 811
悲哀的现实
悲哀的现实 2020-12-05 05:12

I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.

This is what I have tried -

NSString *st         


        
4条回答
  •  暖寄归人
    2020-12-05 06:03

    Here is the Swift 2.x version:

        var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
        var filePath: String = "\(paths[0])/\("index.html")"
    
        // Download and write to file
        var url: NSURL = NSURL(string: "http://www.google.nl")!
        var urlData: NSData = NSData.dataWithContentsOfURL(url)
        urlData.writeToFile(filePath, atomically: true)
    
        // Load file in UIWebView
        web.loadRequest(NSURLRequest(URL: NSURL.fileURLWithPath(filePath)))
    

提交回复
热议问题