Document directory path change when rebuild application

时间:2017-12-18 07:54:46

标签: ios swift filepath nsdocumentdirectory

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

<dependency>
      <groupId>org.apache.spark</groupId>
      <artifactId>spark-core_${scala.binary.version}</artifactId>
      <version>${spark.version}</version>
    </dependency>

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?

3 个答案:

答案 0 :(得分:22)

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.

//below is the innerSync map. recSet is returned from DB call in the format : []map[string]interface{}
var innerSyncMap = sync.Map{}
for _, record := range recSet {

    sKey := record["key"].(string)
    value := record["value"]
    innerSyncMap.Store(sKey, value)
}
MySyncGlobalMap.Store(jobID, innerSyncMap)

Once you have 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) 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

答案 1 :(得分:3)

答案 2 :(得分:0)

关于接受的答案尚不清楚的一件事是,当您想从此处获取文件时,实际上是如何在文档目录中搜索文件。我们开始:

要保存文件(例如图片):

func saveImage(with imageName: String) {
    guard let documentsDirectory = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first else {return}
    guard let data = image.jpegData(compressionQuality: 1) else {return}
    let url = documentsDirectory.appendingPathComponent(imageName)
    do {
        try data.write(to: url)
    } catch {
        print("Error saving image")
    }
    
}

要获取文件:

func fetchImage(with imageName: String) -> UIImage? {
    let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)
    if let path = paths.first {
        let imageURL = URL(fileURLWithPath: path).appendingPathComponent(imageName)
        let data = try! Data(contentsOf: imageURL)
        if let image = UIImage(data: data) {
            return image
        }
    }
    return nil
}

再次重申:这个想法不是要保存文件的完整路径,因为当您尝试从该路径重新启动后尝试获取文件时,将无法正常工作。相反,您应该做的是保存文件的名称(上面示例中的imageName)。然后搜索具有该名称的文件。