I save some audio. The path to the url is
"/Users/abc/Library/Developer/CoreSimulator/Devices/C6751406-604F-4AF4-8D3F-DA26E208632D/data/Containers/Data/Application/7255C72B-F194-49F6-A73D-742E8656F9F5/Library/Caches/-MqPZ42xaixGtyKX06n9.m4a"
To keep things short I eventually save that url to the Documents Directory and keep a copy of that path in Core Data. When I kill the app and restart it again the path has changed. I've read a bunch of answers, most in Obj-C that says to keep a copy of the RelativePath and not the full path in CoreData. That way no matter what happens I can still access the audio when the app is shut down and reopened. I need to pull/construct that Relative Path from CoreData.
I followed this answer and this answer and I keep getting this which is no different than the above url except that it's in the Documents Directory
/Users/me/Library/Developer/CoreSimulator/Devices/C6751406-604F-4AF4-8D3F-DA26E208632D/data/Containers/Data/Application/2014F339-7CF0-4C04-B57E-61305914D3EC/Documents/MyFolder/-MqPZ42xaixGtyKX06n9.m4a
How do I get the Relative Path?
1)
let sourceURL = URL(fileURLWithPath: "/Users/abc/Library/Developer/CoreSimulator/Devices/C6751406-604F-4AF4-8D3F-DA26E208632D/data/Containers/Data/Application/7255C72B-F194-49F6-A73D-742E8656F9F5/Library/Caches/-MqPZ42xaixGtyKX06n9.m4a")
let lastPathComponent = sourceURL.lastPathComponent
let directoryURL: URL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0]
let folderPath: URL = directoryURL.appendingPathComponent("MyFolder", isDirectory: true)
let fileURL: URL = folderPath.appendingPathComponent(lastPathComponent)
do {
try FileManager.default.createDirectory(at: folderPath, withIntermediateDirectories: true, attributes: nil)
try FileManager.default.copyItem(at: sourceURL, to: fileURL)
// save fileURL.path to Core Data
} catch {
}
let sourceURL = URL(fileURLWithPath: "/Users/abc/Library/Developer/CoreSimulator/Devices/C6751406-604F-4AF4-8D3F-DA26E208632D/data/Containers/Data/Application/7255C72B-F194-49F6-A73D-742E8656F9F5/Library/Caches/-MqPZ42xaixGtyKX06n9.m4a")
let lastPathComponent = sourceURL.lastPathComponent
do {
let destFolderURL = try FileManager.default.url(for: .documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("MyFolder")
let fullDestURL = destFolderURL.appendingPathComponent(lastPathComponent)
try FileManager.default.createDirectory(at: destFolderURL, withIntermediateDirectories: true, attributes: nil)
try FileManager.default.copyItem(at: sourceURL, to: fullDestURL)
// save fullDestURL.path to Core Data
} catch {
}