如何在Swift中从文档选择器中保存所选的固件文件。

huangapple go评论68阅读模式
英文:

How to save firmware file selected from document picker in swift

问题

我试图使用固件文件升级雷达。我使用文档选择器选择了一个固件文件,并将其路径保存到一个单例类变量中。但问题是,当我切换到后台然后返回时,它显示文件在该路径不存在。我该如何解决这个问题?用于保存路径的代码是:

let file = urls[0]
do {
    let filePath = file.path
    self.firmwareLocalfilePath = filePath
    SessionController.sharedController.filePath = self.firmwareLocalfilePath
}

这里的SessionController.sharedController.filePath是我的单例变量。用于检查文件是否存在的条件是:

if FileManager().fileExists(atPath: self.firmwareLocalfilePath!)

当我切换到后台然后返回时,它显示“文件不存在[/private/var/mobile/Containers/Data/Application/92D35520-C1E8-44DD-960E-B7024E8602E8/tmp/com.houston-radar.app-Inbox/dc300b04_v297.frm]”。

还有很多其他内容,所以我没有上传完整的代码。这段代码与我在这里写的文件有关。我该如何解决这个问题?我可以将文件保存在像UserDefaults这样的地方吗?我搜索了很多,但没有找到解决方案。感谢您的帮助。

英文:

I am trying to upgrade the radar using a firmware file. I am selecting a firmware file using a document picker and saving its path to a singleton class variable. But the problem is when I go to the background and return it shows that the file does not exist in that path. How can I resolve the issue? The code for saving the path is
`

    let file = urls[0]
    do {
         let filePath = file.path

         self.firmwareLocalfilePath = filePath

         SessionController.sharedController.filePath = self.firmwareLocalfilePath

'

Here SessionController.sharedController.filePath is my singleton variable. The condition to check file availability is

if FileManager().fileExists(atPath: self.firmwareLocalfilePath!)

When I go to the background and come back it is getting that
"File does not exist [/private/var/mobile/Containers/Data/Application/92D35520-C1E8-44DD-960E-B7024E8602E8/tmp/com.houston-radar.app-Inbox/dc300b04_v297.frm]"

There are lots of other kinds of stuff there that's why I am not uploading the full code. The code is related to the file I wrote here. How can I resolve this issue? Can I save the file somewhere like userdefault. I googled it a lot but did not get any solution. Thank you for your help

答案1

得分: 1

你的文件被加载/存储在一个 临时 目录中。iOS 系统自己决定何时从该目录中删除文件。看起来你当前在切换到后台时遇到了这个问题。我建议尝试最初将它保存在一个非临时目录中,但我不确定是否可能。因此,你可以尝试将它从临时文件夹移动到一个非临时文件夹,比如 Application Support 文件夹。

do {
    // 1. 获取固件文件所在的固件URL(你当前的临时URL)
    let firmwarePath: URL = NSURL.fileURL(withPath: localFilePath)

    // 2. 创建一个持久性URL
    let newFirmwarePath = try FileManager.default
        .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        .appendingPathComponent(firmwarePath.lastPathComponent)

    // 3. 尝试将文件从临时目录移动到非临时目录
    //.   这是我们将它从 /tmp 移动到 /Application Support 的地方
    try FileManager.default.moveItem(at: firmwarePath, to: newFirmwarePath)

    // 4. 最后,更新你的静态属性
    SessionController.sharedController.filePath = newFirmwarePath.path
} catch {
    print(error)
}

我觉得以这种方式存储路径很容易出错,所以我只能建议尝试找出另一种与文件/路径/位置通信的替代方法。但我想这应该能够工作。

英文:

Your file is loaded/stored in a temporary directory. iOS itself decides when it wants to remove files from that directory. It appears you're currently experiencing it while going to the background. I would recommend trying to initially save it in a non temporary directory, but I'm not sure if that's possible. Therefore, you could try to move it from the temporary folder to a non temporary folder, such as the Application Support folder.

do {
    // 1. Get the firmware URL at which the firmware file is located 
    //    (your current temporary URL)
    let firmwarePath: URL = NSURL.fileURL(withPath: localFilePath)

    // 2. Create a persistent URL
    let newFirmwarePath = try FileManager.default
        .url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        .appendingPathComponent(firmwarePath.lastPathComponent)

    // 3. Try moving the file from the temporary directory to a non temporary directory
    //.   This is where we move it from the /tmp to /Application Support
    try FileManager.default.moveItem(at: firmwarePath, to: newFirmwarePath)

    // 4. Lastly, update your static property
    SessionController.sharedController.filePath = newFirmwarePath.path
} catch {
    print(error)
}

I feel like storing the path this way is very error prone, so I can only recommend trying to figure out an alternative way to communicate the file/path/location. But I suppose this should work

huangapple
  • 本文由 发表于 2023年2月27日 17:04:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75578500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定