更新应用程序后,iOS上的本地文件会如何处理? [英] What happens to local files on iOS when the app is updated?

查看:48
本文介绍了更新应用程序后,iOS上的本地文件会如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的iOS应用正在将本地文件写入:

My iOS app is writing to local files to:

/var/mobile/Containers/Data/Application/A.../Library/a.txt

我的应用程序更新时,它会获得一个新的应用程序容器ID和文件夹:

When my app updates, it gets a new application container ID and folder:

/var/mobile/Containers/Data/Application/B...

我写到容器ID A的所有文件会怎样?

What happens to all the files I wrote to container ID A?

是否有一个更新挂钩",可以将所有"A"容器文件复制到路径"B"?

Is there an "update hook" that will allow me to copy all the "A" container files to path "B"?

新版本的应用程序(B)可以读取旧版本的文件(A)吗?

Can the new version of the app (B) read the old versions files (A)?

是否有任何文档说明更新过程中文件系统发生了什么情况?

Is there any documentation around what happens to the filesystem during updates?

安装B之后是否可以从容器A恢复文件?

Is it possible to recover files from container A after B has been installed?

推荐答案

更新应用程序时,通过更改.plist文件中的版本号,iOS会使用不同的十六进制名称为该应用程序创建一个新目录并将其复制所有文件都保存到新目录中.现在,如果您使用绝对路径从目录中获取文件的详细信息,则这些路径将是错误的,并且您将不会获得文件的详细信息.

When you update an app, by changing its version number in the .plist file, iOS creates a new directory for that app with a different hexadecimal name and it copies all the files to the new directories. Now if you are using the absolute paths to get the details of files from the directories then those paths would be incorrect and you won't get the file details.

我刚刚在模拟器中尝试过.我创建了如下函数,该函数将返回文档目录路径url

I just tried this in simulator. I created a function as below which will return the document directory path url

func getDocumentsDirectory() -> URL {

    let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
    let documentsDirectory = paths[0]
    return documentsDirectory
}

当版本号为1.0且返回以下路径时,我从应用程序委托中的didFinishLaunchingWithOptions调用了此函数:

I called this function from didFinishLaunchingWithOptions in app delegate when version number was 1.0 and it returned the below path :

file:///Users/BhargavRathod/Library/Developer/CoreSimulator/Devices/3082611F-BCCA-4D17-B390-E0EF4CA454DA/data/Containers/Data/Application/72759097-38F3-4292-825E-1D2343219973/Documents/

当我将版本号更新为1.1时,它为我返回了新路径:

When I updated the version number to 1.1 it returned me the new path as :

file:///Users/BhargavRathod/Library/Developer/CoreSimulator/Devices/3082611F-BCCA-4D17-B390-E0EF4CA454DA/data/Containers/Data/Application/72DC31E9-C32F-42CC-8449-F1946ADB1018/Documents/

因此,如果您使用绝对路径从文档目录访问任何文件,那么这不是一个好习惯.您可以只保存文件名(或文档目录后的相对路径),只要需要访问该文件名,就可以获取文件名并将其附加在文档目录路径后并访问文件.

So if you are using a absolute path to access any file from document directory then it is not good practice. You can just save the file name(or relative path after the document directory) and whenever the access to that file name is required then get the file name and append it after the document directory path and access the file.

我希望这会对您有所帮助.

I hope this will be of some help to you.

这篇关于更新应用程序后,iOS上的本地文件会如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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