该方法从 iOS 14 起已弃用,因此需要 iOS 14 及更高版本的支持。以及 iOS 13 及更早版本。
在我的应用程序上,我曾经UIDocumentPickerViewController允许用户选择文件(导入),但是从iOS 13开始,该功能停止运行,基本上文档选择器处于打开状态,但用户无法选择文件(对文件进行任何操作均不执行任何操作) 。
我制作了一个简单的示例来隔离代码:
class ViewController: UIViewController, UIDocumentPickerDelegate {
@IBAction func openDocumentPicker(_ sender: Any) {
let types = [String(kUTTypePDF)]
let documentPickerViewController = UIDocumentPickerViewController(documentTypes: types, in: .import)
documentPickerViewController.delegate = self
present(documentPickerViewController, animated: true, completion: nil)
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
print("Cancelled")
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
print("didPickDocuments at \(urls)")
}
}
Run Code Online (Sandbox Code Playgroud)
示例项目:https : //github.com/Abreu0101/document-picker-iOS13-issue
我有一个 iOS 应用程序,它试图从外部存储设备读取文件而不将它们导入应用程序的沙箱。
我已按照此处概述的 Apple 文档来执行此操作-
我能够检索选定的目录(位于通过 Lightning 端口连接的外部存储设备上)并枚举目录中的文件。
但是,当我尝试按照推荐的模式对这些文件执行某些操作时,我遇到了失败,并且基本上会出现该文件的权限错误。
let shouldStopAccessing = pickedFolderURL.startAccessingSecurityScopedResource()
defer {
if shouldStopAccessing {
pickedFolderURL.stopAccessingSecurityScopedResource()
}
}
var coordinatedError:NSError?
NSFileCoordinator().coordinate(readingItemAt: pickedFolderURL, error: &coordinatedError) { (folderURL) in
let keys : [URLResourceKey] = [.isDirectoryKey]
let fileList = FileManager.default.enumerator(at: pickedFolderURL, includingPropertiesForKeys: keys)!
for case let file as URL in fileList {
if !file.hasDirectoryPath {
do {
// Start accessing a security-scoped resource.
guard file.startAccessingSecurityScopedResource() else {
// Handle the failure here.
//THIS ALWAYS FAILS!!
return
}
// …Run Code Online (Sandbox Code Playgroud) ios nsfilecoordinator swift uidocumentpickerviewcontroller ios13
有没有办法将所有不同的UTType 扩展类型获取为字符串?我专门需要它们来处理图像、音频和视频。
我遵循了这个答案,但它没有给我所有的扩展
var types = [String]()
let utiTypes = [kUTTypeImage, kUTTypeMovie, kUTTypeVideo, kUTTypeMP3, kUTTypeAudio, kUTTypeQuickTimeMovie, kUTTypeMPEG, kUTTypeMPEG2Video, kUTTypeMPEG2TransportStream, kUTTypeMPEG4, kUTTypeMPEG4Audio, kUTTypeAppleProtectedMPEG4Audio, kUTTypeAppleProtectedMPEG4Video, kUTTypeAVIMovie, kUTTypeAudioInterchangeFileFormat, kUTTypeWaveformAudio, kUTTypeMIDIAudio, kUTTypeLivePhoto, kUTTypeTIFF, kUTTypeGIF, kUTTypeQuickTimeImage, kUTTypeAppleICNS]
for type in utiTypes {
let str = String(type)
guard let utiStr = fileExtension(for: str) else { continue }
types.appent(utiStr)
}
dump(types)
Run Code Online (Sandbox Code Playgroud)
结果是
15 elements // there are really 21 types
- "jpeg"
- "png"
- "mov"
- "mpg"
- "m2v"
- "ts" …Run Code Online (Sandbox Code Playgroud) ios swift uidocumentpickerviewcontroller mobilecoreservices uttype
我正在尝试下载文件夹层次结构中包含的文件UIDocumentPickerController.我用我从获取URL documentPicker:DidPickDocumentsAtURLs:用NSFileCoordinator.
对于iCloud Drive文件提供程序提供的文件夹,它在访问块中提供的URL显然是.zip文件 - 如果文件最初未下载 - 仅包含foo.icloud归档NSURL的文件.如何在压缩过程之前确保已下载这些文件?
我是否应该手动取消归档并将它们再次送入NSFileCoordinator?或者我需要使用startDownloadingUbiquitousItemAtURL:error:?我认为使用它UIDocumentPickerController应该让我摆脱所有的手动协调逻辑.
我错过了什么?
ios nsfilecoordinator uidocumentpickervc icloud-drive uidocumentpickerviewcontroller
我可以UIImagePickerController使用UIDocumentPickerViewController. 但我的问题是,有没有办法用相同的选择器选择图库图像和 pdf 文档?
我试图UIDocumentPickerViewController用documentTypes为kUTTypePDF和kUTTypeImage。但它只打开带有 iCloud 驱动器的文件目录和我的 iPhone 目录。
let docPicker = UIDocumentPickerViewController(documentTypes: [kUTTypePDF as String, kUTTypeImage as String], in: .import)
Run Code Online (Sandbox Code Playgroud)
有什么方法可以用一个选择器同时选择图库或相机胶卷图像和 pdf 文档?
我正在使用带有导入模式的UIDocumentPickerViewController(UIDocumentPickerModeImport),以便让用户从云存储提供程序(dropbox等)导入文档.每次用户重复导入过程时,他都必须从其云存储的根目录开始浏览其文件夹层次结构.
有没有办法在给定的子目录上显示文档选择器?
我知道这个方法:
[NSURL bookmarkDataWithOptions:includingResourceValuesForKeys:relativeToURL:error:]
Run Code Online (Sandbox Code Playgroud)
但我无法用文件选择器演示选项连接点.我希望选择器选项能够在给定的书签目录URL上显示文档选择器.
objective-c ios uidocumentpickervc icloud-drive uidocumentpickerviewcontroller
我有一个自定义文件类型,我试图允许我的应用程序从 UIDocumentPickerViewController 打开。然而,它不起作用。(这里似乎描述了类似的情况和问题,但没有可行的解决方案。)
在我的 Info.plist 我有:
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>myext</string>
</array>
<key>CFBundleTypeIconFile</key>
<string>document.icns</string>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>My document</string>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>LSItemContentTypes</key>
<array>
<string>com.mycompany.app.document</string>
</array>
<key>NSPersistentStoreTypeKey</key>
<string>Binary</string>
</dict>
</array>
Run Code Online (Sandbox Code Playgroud)
和:
<key>UTImportedTypeDeclarations</key>
<array>
<dict>
<key>UTTypeConformsTo</key>
<array/>
<key>UTTypeDescription</key>
<string>My document</string>
<key>UTTypeIdentifier</key>
<string>com.mycompany.app.document</string>
<key>UTTypeSize320IconFile</key>
<string>document</string>
<key>UTTypeSize64IconFile</key>
<string>document</string>
<key>public.filename-extension</key>
<array>
<string>myext</string>
<string>MYEXT</string>
</array>
</dict>
</array>
Run Code Online (Sandbox Code Playgroud)
我正在用以下方法实例化:
let picker = UIDocumentPickerViewController(documentTypes: ["com.mycompany.app.document"],
in: UIDocumentPickerMode.import)
picker.delegate = self
picker.modalPresentationStyle = UIModalPresentationStyle.fullScreen
self.present(picker, animated: true, completion: nil)
Run Code Online (Sandbox Code Playgroud)
问题是,我的 iCloud 帐户中的“测试文件 I.myext”看起来像这样:
即,变灰、不可选择、无法识别的文件。 …
我在iOS 11的UIDocumentPicker导航栏中发现了一个问题,完成,取消或编辑按钮是不可见的,当用户触摸它时,它会出现即正常状态下的颜色为白色,即使在更改时也是如此UINavigationBar.appearnce().tintColor,仅颜色触摸后改变了.
我正在使用 UIDocumentPickerViewController 从文件中选择文档并将其上传到服务器。我能够成功访问文件,但是在单击文件时,不会调用委托方法。
我使用以下代码来调用文档选择器:
class Uploads: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func uploadDocument(_ sender: Any) {
let documentPicker = UIDocumentPickerViewController(documentTypes: [String(kUTTypePDF), String(kUTTypePlainText)], in: .import)
documentPicker.delegate = self
if #available(iOS 11.0, *) {
documentPicker.allowsMultipleSelection = false
} else {
}
present(documentPicker, animated: true, completion: nil)
}
/*
// MARK: - Navigation
// In …Run Code Online (Sandbox Code Playgroud) uidocumentpickerviewcontroller ×10
ios ×8
swift ×5
icloud-drive ×2
ios13 ×2
delegates ×1
ios11 ×1
objective-c ×1
swift5 ×1
uti ×1
uttype ×1