Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
268 views
in Technique[技术] by (71.8m points)

ios - Document directory path change when rebuild application

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

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 - 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 - 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

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

how can solve this problem?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

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

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...