重建应用程序时文档目录路径更改 [英] Document directory path change when rebuild application

查看:84
本文介绍了重建应用程序时文档目录路径更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从url下载视频文件,并使用以下路径将其保存在文档目录中:

I download the video file from url and save it in document directory with this path:

  let destination: DownloadRequest.DownloadFileDestination = { _, _ in
      let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"
      let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
      let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
      let fileURL: URL = folderPath.appendingPathComponent(pathComponent)
      return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
    }

我的视频已下载并成功播放. 但是有一个问题,当我在Xcode中重建应用程序并尝试播放我下载的最后一个视频时,视频不会显示,而当我下载一个新视频时,此保存成功地播放了.

my video is downloaded and plays successfully. but there is a problem, when I rebuild application in Xcode and try to play the last video that I downloaded, video is not shown, and when I download a new video this save and play successfully.

我看过每个视频捆绑包路径,它们是不同的.

I've seen each video bundle path, they are different.

1-文件:///Users/myMac/Library/Developer/CoreSimulator/Devices/ EAC2F4CE-EA09-46C0-B403-1CE9E24B6822 /data/Containers/Data/Application/1D2C1F7B-E627 -4898-91C1-D0AF8D5E0F1E/Documents/Downloads/pack7-1.mp4

1 - file:///Users/myMac/Library/Developer/CoreSimulator/Devices/EAC2F4CE-EA09-46C0-B403-1CE9E24B6822/data/Containers/Data/Application/1D2C1F7B-E627-4898-91C1-D0AF8D5E0F1E/Documents/Downloads/pack7-1.mp4

2-文件:///Users/myMac/Library/Developer/CoreSimulator/Devices/EAC2F4CE-EA09-46C0-B403-1CE9E24B6822/data/Containers/Data/Application/ F950E9A5-C9F3-4B8C-BCF5 -647EEC233CEE /Documents/Downloads/pack7-3.mp4

2 - file:///Users/myMac/Library/Developer/CoreSimulator/Devices/EAC2F4CE-EA09-46C0-B403-1CE9E24B6822/data/Containers/Data/Application/F950E9A5-C9F3-4B8C-BCF5-647EEC233CEE/Documents/Downloads/pack7-3.mp4

现在,我的问题是,当我们从App Store更新应用程序时,是否意味着要重新安装?这条路径会改变吗?

Now, my question is, when we update the app from the App Store, it means a reinstallation? Does this path change?

如何解决这个问题?

推荐答案

从iOS 8开始,每次重新启动应用程序时,指向应用程序沙箱的绝对URL都会更改.因此,您绝对不应保存视频的绝对网址.保存视频名称,并在每次重新启动应用程序时重新创建网址.

iOS 8 onwards, Absolute url to app's sandbox changes every time you relaunch the app. Hence you should never save the absolute url of the video. Save the name of the video and recreate the url every time you relaunch the app.

  let pathComponent = "pack\(self.packID)-\(selectRow + 1).mp4"
  let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
  let folderPath: URL = directoryURL.appendingPathComponent("Downloads", isDirectory: true)
  let fileURL: URL = folderPath.appendingPathComponent(pathComponent)

一旦您找到了fileURL文件,您将找到在先前的启动中下载的文件.

Once you have fileURL look for the file and you will find the file downloaded in previous launch.

每次用户启动应用程序时,iOS都会为应用程序创建一个新的沙箱.因此,绝对URL将非常有用.但是,iOS会像以前一样负责在沙盒中设置所有文件夹和内容.因此,尽管SandBox的基本URL发生了变化,但所有内容的相对URL都将保持不变.

iOS creates a new Sandbox for app every time user launches the app. Hence absolute URL will very. But iOS will take care of setting up all the folders and contents inside the Sandbox as it was earlier. So though base url of SandBox change, relative url's of all the content will be remained intact.

因此建议不要将绝对URL保存到任何文件夹中:)希望它会有所帮助

Hence its advised never to save absolute url to any folder :) Hope it helps

这篇关于重建应用程序时文档目录路径更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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