Nic*_*mer 5 uidocument uidocumentpicker
设置文档选择器,用户可以从“文件应用程序”中选择 PDF 文件,并将其保存到我的应用程序文档文件夹中以在应用程序中使用。
打开文档选择器,选择文件 - 但关闭后出现以下错误:
“[DocumentManager] 视图服务确实终止并出现错误:Error Domain=_UIViewServiceErrorDomain Code=1 “(null)”UserInfo={Termminate=disconnect method}”
UIViewController代码:
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]
}
Run Code Online (Sandbox Code Playgroud)
}
我已经查过这个错误,但似乎无法纠正。这与 iCloud 功能有关吗?如果是这样,这是因为文件文档可能位于个人 iCloud 上吗?因为我不打算将任何文档保存到 iCloud,只保存到应用程序文档文件夹。
如果此错误是由于 iCloud 造成的,我已按照 Apple 文档进行操作:https: //developer.apple.com/documentation/xcode/configuring-icloud-services
并设置如下:
我注意到 iCloud 功能是
但是错误仍然显示。有任何想法吗?提前致谢
| 归档时间: |
|
| 查看次数: |
298 次 |
| 最近记录: |