【问题标题】:Alamofire download to file system doesn't workAlamofire 下载到文件系统不起作用
【发布时间】:2018-07-22 17:50:50
【问题描述】:

在将 Alamofire 下载到我们的应用程序之前,我正在创建一个测试项目。现在,我面临一个奇怪的问题,也许我不明白 Alamofire 下载或 FileManager 的工作原理。

我只是从一个 URL 下载 3 个文件,并希望将它们存储在文件系统中。之后,我想将它们用作 WKWebView 中的本地数据(尚未实现)。

这是我的视图控制器:

import UIKit

class ViewController: UIViewController {

    @IBAction func loadWebsite(_ sender: Any) {     
        NetworkService.shared.getFile(path: "webview.html", completion: {
            NetworkService.shared.getFile(path: "css/style.css", completion: {
                NetworkService.shared.getFile(path: "images/square.jpg", completion: {
                    self.fillInContent()
                })
            })
        })
    }

    private func fillInContent() {
        let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
        let fileUrl = documentURL.appendingPathComponent("webview.html")

        if FileManager.default.fileExists(atPath: fileUrl.absoluteString) {
            print("True")
        } else {
            print("no webview")
        }
    }
}

我知道加载资源的代码在完成块中启动新请求时写得不是很好,但它只是一个测试项目。

这是我的 NetworkService 单例:

import UIKit
import Alamofire

class NetworkService {
    static let shared = NetworkService()

    // Thread-safe
    private init() {}

    func getFile(path: String, completion: @escaping() -> Void) {
        let destination: DownloadRequest.DownloadFileDestination = {_, _ in
            let documentURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
            let fileURL = documentURL.appendingPathComponent(path)

            return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
        }

        Alamofire.download("https://myurl.com/" + path, to: destination)
            .responseData { response in
                if let error = response.result.error {
                    print("Error: \(error.localizedDescription)")
                }

               completion()
        }
    }
}

当我打印出 NetworkService 中的文件 URL 或 VC 中的 fillInContent() 时,它们是正确的。但是,fillInContent() 方法总是打印出“no webview”。所以文件不存在。我希望它会被其他完成块覆盖,但即使我在完成块中启动函数,fileExist() 也会返回 false。即使我删除了对 getFile() 的其他 2 个调用,我也不会在这里得到 fileExists 的真实情况。

当前行为:我一直收到“无网络视图”。

预期行为:fillInContent() 方法输出“True”。

非常感谢任何帮助或指导!

编辑: 运行项目两次后,我也收到此错误消息:'错误:文件“文档”无法保存在文件夹“1E07C511-B5B3-4B5A-84E1- 8EF61F6B7340”,因为已经存在同名文件。'

【问题讨论】:

    标签: ios swift alamofire nsfilemanager


    【解决方案1】:

    尝试使用此方法获取文档目录:

    try? FileManager.default.url(for: .documentDirectory,
                                 in: .userDomainMask,
                                 appropriateFor: nil,
                                 create: true)
    

    并使用fileUrl.path 而不是fileUrl.absoluteString

    【讨论】:

    • 感谢您的回答,但结果还是一样:/
    • 更新了答案
    • 嗯,现在 getFile() 中获取文档目录的代码返回 nil。
    • 我编辑了我的问题。我怀疑该错误会导致大部分麻烦,因为我什至没有从请求中收到数据(发生错误时)。但是,我不知道“文档”文件是什么意思。
    • 我想将 fileUrl.absoluteString 更改为 fileUrl.path 解决了我的问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 2013-04-12
    • 2011-03-13
    相关资源
    最近更新 更多