Document directory path change when rebuild application

I suggest using a bookmark. Check out the section Locating Files Using Bookmarks in the following article: https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/FileSystemProgrammingGuide/AccessingFilesandDirectories/AccessingFilesandDirectories.html#//apple_ref/doc/uid/TP40010672-CH3

Edit:

Some relevant parts of the linked document, just in case:

Important: Although they are safe to use while your app is running, file reference URLs are not safe to store and reuse between launches of your app because a file’s ID may change if the system is rebooted. If you want to store the location of a file persistently between launches of your app, create a bookmark as described in Locating Files Using Bookmarks.

I've an app that uses a lot of file handling so I've created the following method in an NSURL category to return bookmark data for a given URL.

- (NSData*)defaultBookmark
{
    NSError* error = nil;
    NSData* bookmarkData =  [self bookmarkDataWithOptions:NSURLBookmarkCreationSuitableForBookmarkFile
                           includingResourceValuesForKeys:nil
                                            relativeToURL:nil
                                                    error:&error];
    if (error != nil)
    {
        NSLog(@"error creating bookmark for url '%@': %@", self, error);
    }
    return bookmarkData;
}

To create a NSURL object from bookmark data use something like:

    NSError* error = nil;
    BOOL isStale = NO;
    NSURL* url = [NSURL URLByResolvingBookmarkData:bookmarkData
                                           options:NSURLBookmarkResolutionWithoutUI
                                     relativeToURL:nil
                               bookmarkDataIsStale:&isStale
                                             error:&error];
    if (error != nil || url == nil)
    {
        NSLog(@"Error restoring url from bookmark: %@", error);
    }
    else
    {
        // use url to load file
        ...
    }

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


One thing that's unclear about accepted answer is how you actually search for file in Document's directory when you want to fetch it from there. Here we go:

To save file (e.g. image):

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

To fetch file:

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
}

So once again: the idea is NOT to save the full path to the file, because when you try to get file after reboot from that path that won't work. What you shall do instead is to save the name of the file (imageName in the example above). And then you search for file with that name.