SwiftUI |在模拟器上找到但在 iOS 设备上找不到的文件 [英] SwiftUI | File found on simulator but not on iOS device

查看:34
本文介绍了SwiftUI |在模拟器上找到但在 iOS 设备上找不到的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的程序和位于应用程序 /Documents 中的数据库之间创建连接.当代码在模拟器上构建使用时,成功打开数据库;但是,当我在 iOS 设备上运行代码时,它找不到该文件.

I'm trying to create a connection between my program and a database located in /Documents of the app. When the code is built using on the simulator, it successfully opens the database; however, when I run the code on an iOS device, it can't find the file.

这是我使用的代码:

let path = NSSearchPathForDirectoriesInDomains(
        .documentDirectory, .userDomainMask, true
    ).first!
    
    let db = try! Connection("\(path)/Database.db")

这些是我在模拟器上运行代码时变量path的内容:

These are the contents of the variable path when I run the code on the simulator:

/Users/xxxx/Library/Developer/CoreSimulator/Devices/EFD14A1B-7207-4840-9ACE-8E44A269CC70/data/Containers/Data/Application/58D150B9-E242-4857-B06C-DA28C88A26D0/Documents

当我在 iOS 设备上运行代码时,这些是变量 path 的内容:

And these are the contents of the variable path when I run the code on an iOS device:

/var/mobile/Containers/Data/Application/4EC93D76-4E99-4552-855A-48C1D9346449/Documents

Xcode 版本:12.0
iOS 设备:iPhone X
iOS 版本:14.1

Xcode version: 12.0
iOS device: iPhone X
iOS version: 14.1

编辑我尝试使用此代码将数据库从应用程序包复制到文档目录,但出现错误 Unable to copy file:

Edit I tried to use this code to copy the database from the app bundle to the document directory, but it gives the error Unable to copy file:

copyFileToDocumentsFolder(nameForFile: "Database", extForFile: "db")


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")
        }
}

推荐答案

正如我在上一个问题中已经提到的,Bundle 是只读的.您需要将数据库移动/复制到另一个可以读/写的目录.如果您不希望用户访问该文件,则应将其复制到该应用程序支持目录:

As I already mentioned in your last question the Bundle is read-only. you need to move/copy your database to another directory that you can read/write to it. If you don't want the user to have access to the file you should copy it to that application support directory:

extension URL {
    static let database: URL = {
        let applicationSupport = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
        let bundleID = Bundle.main.bundleIdentifier ?? "company name"
        let subDirectory = applicationSupport.appendingPathComponent(bundleID, isDirectory: true)

        let destination = subDirectory.appendingPathComponent("Database.db")
        if !FileManager.default.fileExists(atPath: destination.path) {

            let source = Bundle.main.url(forResource: "Database", withExtension: "db")!
            do {
                try FileManager.default.createDirectory(at: subDirectory, withIntermediateDirectories: true, attributes: nil)
                print("directory created")
                try FileManager.default.copyItem(at: source, to: destination)
                print("file copied successfully")
            } catch {
                print("Unable to copy file. return the bundle read-only version")
                return source
            }
        }
        print("database found return app suport database read-write url")
        return destination
    }()
}


let dbURL = URL.database

这篇关于SwiftUI |在模拟器上找到但在 iOS 设备上找不到的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆