SwiftUI fileImporter won't call onCompletion under macOS

186 Views Asked by At

If I call file importer with a State variable over my toolbar icon, the fileImporter will be correctly opened in iOS, iPadOS and macOS. If I select a picture (double click, click open, etc.) the .onCompletion will be correctly called only under iOS and iPadOS. macOS on the other hand, will never reach that. The state variable is called under macOS though. Therefore I think, that macOS dismisses the view whenever I selected an image.

whether the switch state .success, neither .failure will be reached

Maybe there is some help for me out there :)

Additional Note! **Access for Files is allowed ** So sadly this is not the reason for this malfunction

.toolbar {
    ToolbarItem() {
        Button {
            self.openFile.toggle()
        } label: {
            Label("Import",systemImage: "square.and.arrow.down")
        }
    }
}
.fileImporter(
    isPresented: $openFile,
    allowedContentTypes: [.image]
){ result in
    switch result {
    case .success(let fileURL):
        print("Test access")
        let gotAccess = fileURL.startAccessingSecurityScopedResource()
        if !gotAccess { return }
        
        pollViewModel.pollContainer.polls[pollIndex].pictures[pictureIndex].pictureDataname = fileURL.lastPathComponent
        
        let templocalImage = importPictureFrom(url: fileURL)!
        pollViewModel.saveImage(image: templocalImage, pollIndex: pollIndex, pictureIndex: pictureIndex)
        localImage = pollViewModel.loadPicture(pollIndex: pollIndex, pictureIndex: pictureIndex)!
        
        fileURL.stopAccessingSecurityScopedResource()
    case .failure(let error):
        // handle error
        print(error)
    }
}
1

There are 1 best solutions below

0
Phil McCarthy On

I solved this by setting the Target App Sandbox File Access Type for user selected files to 'Read/Write'

It fails when set to 'Read Only'

As you explained, the onCancellation closure is called if this is not set to 'Read/Write'