I am using the .fileImporter modifier in SwiftUI to import pdf files in my app. I’m having a few issues. Firstly the data is slow to load and often it doesn’t load and gives message Internal Error Couldn’t communicate with a helper application.
But the main issue is if the modal view is dismissed with a swipe down, it cannot be presented again. Presumably because the binding $showFileImporter is not reset. If the cancel button is pressed to dismiss it works fine. I don’t know if there is anyway to force it to fullScreen to get around this.
Here is my code:
.fileImporter(isPresented: $showFileImporter, allowedContentTypes: [.pdf]) { result in
switch result {
case .success(let url):
url.startAccessingSecurityScopedResource()
if let pDFDocument = PDFDocument(url: url) {
if let data = pDFDocument.dataRepresentation() {
// handle data
}
}
case .failure(let error):
print(error)
}
}
2
Answers
I can confirm my .fileImporter also does not reset the isPresented binding on swipe-down. One way to be for certain is to change your button that presents the file importer from
$showFileImporter = true
to$showFileImporter.toggle()
. On swiping to dismiss the file importer, you have to press the button twice to present again.Unfortunately this seems to be yet another half-baked SwiftUI thing, but I did find a suitable work-around – handling this case with the presenting button itself:
I tried just calling .toggle() twice in a row sequentially but needed a slight delay for the trigger to register.
Edit:
I also noticed you didn’t call
url.stopAccessingSecurityScopedResource()
, which you should do after calling to yourpDFDocument
.The issue persists as of August 2022. I needed to implement Joseph Dvorak’s most excellent fix in multiple places so abstracted it on to any equatable binding: