1

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 {
}
Lance Samaria
  • 17,576
  • 18
  • 108
  • 256
  • 1
    Relative path means to save `/MyFolder/-MqPZ42xaixGtyKX06n9.m4a` in Core Data and concatenate this path with the current path to the documents directory. – vadian Dec 22 '21 at 06:04
  • @vadian thanks, I'll try it. 1 question- 1) `lastPathComponent` gives me `/-MqPZ42xaixGtyKX06n9.m4a`, is there a way to automatically get `/MyFolder` along with it or do I need to put together both parts myself? – Lance Samaria Dec 22 '21 at 09:13
  • 1
    if `MyFolder` is hardcoded anyway you could use also a string literal. For example `"/MyFolder/\{fullDestURL.lastPathComponent)"` – vadian Dec 22 '21 at 09:18
  • Both of your comments work. Thanks for the help! Maybe you should post as answer so that it can help others. – Lance Samaria Dec 22 '21 at 11:21

0 Answers0