Setting up a document picker where users can select PDF files from the 'Files app' and save this to my app document folder to use in the app.
Got the document picker to open, select file - but upon closing I get the following error:
"[DocumentManager] The view service did terminate with error: Error Domain=_UIViewServiceErrorDomain Code=1 "(null)" UserInfo={Terminated=disconnect method}"
UIViewController code:
import UIKit
import CoreData
import PDFKit
import MobileCoreServices
import UniformTypeIdentifiers
class AddDocumentViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UIDocumentPickerDelegate {
@IBAction func chooseFile (_ sender: UIButton) {
fileTypeSelectionInput = fileTypeSelection.selectedSegmentIndex
var documentPicker: UIDocumentPickerViewController!
if #available(iOS 14, *) {
// iOS 14 & later
let supportedTypes: [UTType] = [UTType.pdf]
documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes)
} else {
// iOS 13 or older code
let supportedTypes: [String] = [kUTTypeImage as String]
documentPicker = UIDocumentPickerViewController(documentTypes: supportedTypes, in: .import)
}
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
documentPicker.modalPresentationStyle = .formSheet
self.present(documentPicker, animated: true)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let fileURL = urls[0]
var fileName = urls[0].lastPathComponent
subjectFileName = fileName
let filePath = getDocumentsDirectory().appendingPathComponent(fileName)
print(fileName)
guard fileURL.startAccessingSecurityScopedResource() else {
// Handle denied access
return
}
defer { fileURL.stopAccessingSecurityScopedResource() }
do {
guard let document = PDFDocument(url: fileURL) else {
print("Error: could not create PDFDocument from url")
return
}
document.write(to: filePath)
print ("success")
} catch let error {
print("error: \(error)")
}
}
func getDocumentsDirectory() -> URL {
let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
return paths[0]
}
}
I've looked up this error but I can't seem to remedy it. Is this linked with iCloud capability? If so, is this due to fact that File documents could be on the personal iCloud? Because I'm not planning to save any documents to iCloud, only to the app documents folder.
If this error is due to iCloud, I've followed the Apple documentation on this: https://developer.apple.com/documentation/xcode/configuring-icloud-services
And set up as below:
I note that iCloud capability is
However the error keeps showing. Any ideas? Thanks in advance