невозможно отобразить HTML-файл в веб-просмотре из каталога документов - PullRequest
0 голосов
/ 05 апреля 2019

Я хочу отобразить HTML-файл из папки в каталоге документов. Я скачал zip-файл, чем распаковать его, теперь я хочу отобразить html-файл в UIWebView.

Я скачал файл с этого URL: http://MyURL/D_Word_Meaning_Quiz/data_zip.zip

Скачать файл так: -

static func loadFileAsync(url: URL, completion: @escaping (String?, Error?) -> Void) {
        let documentsUrl =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)

        if FileManager().fileExists(atPath: destinationUrl.path) {
            completion(destinationUrl.path, nil)
        } else {
            let session = URLSession(configuration: URLSessionConfiguration.default, delegate: nil, delegateQueue: nil)
            var request = URLRequest(url: url)
            request.httpMethod = "GET"
            let task = session.dataTask(with: request, completionHandler:
            {
                data, response, error in
                if error == nil
                {
                    if let response = response as? HTTPURLResponse
                    {
                        if response.statusCode == 200
                        {
                            if let data = data
                            {
                                if let _ = try? data.write(to: destinationUrl, options: Data.WritingOptions.atomic)
                                {
                                    completion(destinationUrl.path, error)
                                }
                                else
                                {
                                    completion(destinationUrl.path, error)
                                }
                            }
                            else
                            {
                                completion(destinationUrl.path, error)
                            }
                        }
                    }
                }
                else
                {
                    completion(destinationUrl.path, error)
                }
            })
            task.resume()
        }
    }

Распакуйте файл следующим образом: -

func unzipFolder(filePath: String){
        do {
            let convertedURL = URL.init(fileURLWithPath: filePath)
            print(convertedURL)
            let unzipDirectory = try Zip.quickUnzipFile(convertedURL)
            print(unzipDirectory)
            let htmlUrl = unzipDirectory.appendingPathComponent("index.html")
            let req = URLRequest(url: htmlUrl)
            DispatchQueue.main.async {
                self.webV.loadRequest(req)
            }

        } catch {
            print("Something went wrong")
        }
    }

Печать каталога документов:

[file:///private/var/mobile/Containers/Data/Application/1CC5FFEA-DF9A-4590-8628-B9873F775568/Documents/data_zip/, file:///private/var/mobile/Containers/Data/Application/1CC5FFEA-DF9A-4590-8628-B9873F775568/Documents/data_zip.zip]

Receiving this error code in console: NSURLConnection finished with error - code -1100
...