![](/img/trans.png)
[英]How to copy a folder of files from IOS App bundle resources to Documents Directory
[英]Copy all the bundle resources to a separate folder inside the Documents directory
復制包中包含的所有文件 (而不是[NSBundle mainBundle]
)並將它們放置到Documents
目錄中新創建的目錄中的正確方法是什么?
您需要一個一個地復制項目,這非常簡單。
let bundle: Bundle = ... // Whatever bundle you want to copy from
guard let resourceURL = bundle.resourceURL else { return }
let fileManager = FileManager.default
do {
let documentsDirectory = try fileManager.url(for: .documentDirectory,
in: .userDomainMask,
appropriateFor: nil,
create: false)
let destination = documentsDirectory.appendingPathComponent("BundleResourcesCopy", isDirectory: true)
var isDirectory: ObjCBool = false
if fileManager.fileExists(atPath: destination.path, isDirectory: &isDirectory) {
assert(isDirectory.boolValue)
} else {
try fileManager.createDirectory(at: destination, withIntermediateDirectories: false)
}
let resources = try fileManager.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)
for resource in resources {
print("Copy \(resource) to \(destination.appendingPathComponent(resource.lastPathComponent))")
try fileManager.copyItem(at: resource,
to: destination.appendingPathComponent(resource.lastPathComponent))
}
} catch {
print(error)
}
根據捆綁軟件的大小,這可能需要一些時間才能執行,因此您可能需要在后台線程上執行此操作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.