动态需要更新。我可以从 URL 下载证书并将其保存到文档目录。
func downloadFile(url: URL, completion: @escaping (String?, Error?) -> Void) {
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
let destinationUrl = documentsUrl.appendingPathComponent(url.lastPathComponent)
if FileManager().fileExists(atPath: destinationUrl)
{
print("File already exists [\(destinationUrl)]")
completion(destinationUrl, nil)
}
if let dataFromURL = NSData(contentsOf: url)
{
if dataFromURL.write(to: URL(string: destinationUrl)!, atomically: true)
{
print("file saved [\(destinationUrl)]")
completion(destinationUrl, nil)
}
else
{
print("error saving file")
let error = NSError(domain:"Error saving file", code:1001, userInfo:nil)
completion(destinationUrl, error)
}
}
else
{
let error = NSError(domain:"Error downloading file", code:1002, userInfo:nil)
completion(destinationUrl, error)
}
}
我的问题是下载文件后需要保存到 App Bundle (Bundle.main.resourcePath) 目录中。
还是保存到文档文件夹,移动到 App Bundle 目录下?