Hi there,
I have an SwiftUI app that opens a user selected audio file (wave). For each audio file an additional file exists containing events that were extracted from the audio file. This additional file has the same filename and uses the extension bcCalls. I load the audio file using FileImporter view modifier and within access the audio file with a security scoped bookmark. That works well. After loading the audio I create a CallsSidecar NSFilePresenter with the url of the audio file. I make the presenter known to the NSFileCoordinator and upon this add it to the FileCoordinator. This fails with NSFileSandboxingRequestRelatedItemExtension: Failed to issue extension for; Error Domain=NSPOSIXErrorDomain Code=3 "No such process"
My Info.plist contains an entry for the document with NSIsRelatedItemType set to YES
I am using this kind of FilePresenter code in various live apps developed some years ago. Now when starting from scratch on a fresh macOS26 system with most current Xcode I do not manage to get it running. Any ideas welcome!
Here is the code:
struct ContentView: View {
@State private var sonaImg: CGImage?
@State private var calls: Array<CallMeasurements> = Array()
@State private var soundContainer: BatSoundContainer?
@State private var importPresented: Bool = false
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
if self.sonaImg != nil {
Image(self.sonaImg!, scale: 1.0, orientation: .left, label: Text("Sonagram"))
}
if !(self.calls.isEmpty) {
List(calls) {aCall in
Text("\(aCall.callNumber)")
}
}
Button("Load sound file") {
importPresented.toggle()
}
}
.fileImporter(isPresented: $importPresented, allowedContentTypes: [.audio, UTType(filenameExtension: "raw")!], onCompletion: { result in
switch result {
case .success(let url):
let gotAccess = url.startAccessingSecurityScopedResource()
if !gotAccess { return }
if let soundContainer = try? BatSoundContainer(with: url) {
self.soundContainer = soundContainer
self.sonaImg = soundContainer.overviewSonagram(expectedWidth: 800)
let callsSidecar = CallsSidecar(withSoundURL: url)
let data = callsSidecar.readData()
print(data)
}
url.stopAccessingSecurityScopedResource()
case .failure(let error):
// handle error
print(error)
}
})
.padding()
}
}
The file presenter according to the WWDC 19 example:
class CallsSidecar: NSObject, NSFilePresenter {
lazy var presentedItemOperationQueue = OperationQueue.main
var primaryPresentedItemURL: URL?
var presentedItemURL: URL?
init(withSoundURL audioURL: URL) {
primaryPresentedItemURL = audioURL
presentedItemURL = audioURL.deletingPathExtension().appendingPathExtension("bcCalls")
}
func readData() -> Data? {
var data: Data?
var error: NSError?
NSFileCoordinator.addFilePresenter(self)
let coordinator = NSFileCoordinator.init(filePresenter: self)
NSFileCoordinator.addFilePresenter(self)
coordinator.coordinate(readingItemAt: presentedItemURL!, options: [], error: &error) {
url in
data = try! Data.init(contentsOf: url)
}
return data
}
}
And from Info.plist
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>bcCalls</string>
</array>
<key>CFBundleTypeName</key>
<string>bcCalls document</string>
<key>CFBundleTypeRole</key>
<string>None</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.apple.property-list</string>
</array>
<key>LSTypeIsPackage</key>
<false/>
<key>NSIsRelatedItemType</key>
<true/>
</dict>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>wav</string>
<string>wave</string>
</array>
<key>CFBundleTypeName</key>
<string>Windows wave</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSHandlerRank</key>
<string>Alternate</string>
<key>LSItemContentTypes</key>
<array>
<string>com.microsoft.waveform-audio</string>
</array>
<key>LSTypeIsPackage</key>
<integer>0</integer>
<key>NSDocumentClass</key>
<string></string>
</dict>
</array>
Note that BatSoundContainer is a custom class for loading audio of various undocumented formats as well as wave, Flac etc. and this is working well displaying a sonogram of the audio.
Thx, Volker