This is a great implementation of the recently announced fileImporter -- thank you!
I've made some modifications for my own code, and find that it runs fine on an iPad/iPhone simulator, but fails on a real device.
So then I downloaded your original code, and found the same behavior: it works on the simulators, but fails on the real device.
By adding 2 lines, under the // Handle failure comment near the end of the .fileImporter block of code:
.fileImporter(
isPresented: $isImporting,
allowedContentTypes: [.plainText],
allowsMultipleSelection: false
) { result in
do {
guard let selectedFile: URL = try result.get().first else { return }
guard let message = String(data: try Data(contentsOf: selectedFile), encoding: .utf8) else { return }
document.message = message
} catch {
// Handle failure.
print("Unable to read file contents")
print(error.localizedDescription)
}
}
I find that there are file permission errors with the real devices:
Unable to read file contents
The file “Message.txt” couldn’t be opened because you don’t have permission to view it.
I've tried this on iPadOS 14.2 beta, and iPhone 14.01. Am I misusing this, or is this an Apple bug?