简体   繁体   English

Swift:首次运行应用程序时,如何将文件从应用程序包复制到“文档”文件夹

[英]Swift: How to copy files from app bundle to Documents folder when app runs for first time

There are all sorts of sample code & questions on SO dealing with how to programmatically copy files in Obj-C from the app bundle to the application's sandboxed Documents folder (eg here , here , and here ) when the application runs for the first time. 关于SO的各种示例代码和问题,涉及如何在应用程序首次运行时以编程方式将Obj-C中的文件从应用程序捆绑包复制到应用程序的沙盒Documents文件夹(例如hereherehere )。

How do you do this in Swift? 您如何在Swift中做到这一点?

You could use FileManager API: 您可以使用FileManager API:

Here's example with a function that copies all files with specified extension: 这是带有复制具有指定扩展名的所有文件的函数的示例:

func copyFilesFromBundleToDocumentsFolderWith(fileExtension: String) {
    if let resPath = Bundle.main.resourcePath {
        do {
            let dirContents = try FileManager.default.contentsOfDirectory(atPath: resPath)
            let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
            let filteredFiles = dirContents.filter{ $0.contains(fileExtension)}
            for fileName in filteredFiles {
                if let documentsURL = documentsURL {
                    let sourceURL = Bundle.main.bundleURL.appendingPathComponent(fileName)
                    let destURL = documentsURL.appendingPathComponent(fileName)
                    do { try FileManager.default.copyItem(at: sourceURL, to: destURL) } catch { }
                }
            }
        } catch { }
    }
}

Usage: 用法:

copyFilesFromBundleToDocumentsFolderWith(fileExtension: ".txt")

For Swift 4.2: 对于Swift 4.2:

Assuming the file in your App Bundle is called Some File.txt 假设您的应用程序捆绑包中的文件名为Some File.txt

In ViewDidLoad , add: ViewDidLoad ,添加:

let docName = "Some File"
let docExt = "txt"
copyFileToDocumentsFolder(nameForFile: docName, extForFile: docExt)

and then create a function as follows: 然后创建一个函数,如下所示:

func copyFileToDocumentsFolder(nameForFile: String, extForFile: String) {

    let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first
    let destURL = documentsURL!.appendingPathComponent(nameForFile).appendingPathExtension(extForFile)
    guard let sourceURL = Bundle.main.url(forResource: nameForFile, withExtension: extForFile)
        else {
            print("Source File not found.")
            return
    }
        let fileManager = FileManager.default
        do {
            try fileManager.copyItem(at: sourceURL, to: destURL)
        } catch {
            print("Unable to copy file")
        }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Cordova将文件从应用程序捆绑包复制到Documents文件夹? - How to copy a file from app bundle to the Documents folder with Cordova? 如何将文件文件夹从IOS App捆绑资源复制到Documents Directory - How to copy a folder of files from IOS App bundle resources to Documents Directory 是否有 API 可以从 iOS 应用程序的文档文件夹中复制文件? - Are there APIs to copy files from an iOS app's documents folder? Swift 3 - 将包含内容的文件夹从主包复制到文档目录 - Swift 3 - Copy Folder w/ contents from Main Bundle to Documents Directory 数据库中存在数据库但不存在于设备构建中(将文件从App Bundle复制到Swift中的文档目录) - Database present in simulator but not on device build (Copying files from App Bundle to Documents Directory in Swift) 从本地应用程序Documents文件夹中删除文件 - Deleting files from local app Documents folder iOS:应用扩展程序如何将文件复制到应用文档/文件夹? - iOS: How can app extension copy file to app Documents/ folder? 如何确定用户第一次运行应用程序? - How to determine that user runs the app for the first time? Swift 中是否有仅在第一次打开应用程序时运行的功能? - Is there a function in Swift that only runs during the first time opening the app? iOS:将文件从 Office 应用复制到我的应用文档 - iOS: Copy files from Office app to my app's documents
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM