我有一个关于没有为 DocumentPickerViewController 调用委托方法的查询,这是背景,我只需要从我的文件应用程序中导入可用的资源,因此我使用 UIDocumentPickerViewController。
我有一个单独的 ViewController,我将 documentPickerViewController 的视图添加为子视图并添加它的委托。我的 ViewController 的代码是这样的。
var documentPickerController: UIDocumentPickerViewController!
let supportedUTI = [kUTTypeImage,kUTTypeSpreadsheet,kUTTypePresentation,kUTTypeDatabase,kUTTypeFolder,kUTTypeZipArchive,kUTTypeVideo, kUTTypeAudiovisualContent]
documentPickerController = UIDocumentPickerViewController.init(documentTypes: supportedUTI as [String], in: .import)
documentPickerController.delegate = self
documentPickerController.allowsMultipleSelection = false
view.addSubview(documentPickerController.view)
Run Code Online (Sandbox Code Playgroud)
现在,当我看到 pickercontroller 已打开,当我点击 Cancel 时会documentPickerWasCancelled被调用,但是当我选择一个文件时documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]不会被调用。
我试图进一步深入了解我所看到的内容,而不是显示我的 ViewController,如果我像这样直接显示 pickerViewController,我会将选择器的视图添加为子视图
UIDocumentPickerViewController *dc = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:[self UTITypes] inMode:UIDocumentPickerModeImport];
dc.delegate = self;
[MainVC presentViewController:dc animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud)
两个委托方法都被调用得很好。我不明白为什么。有人可以在这里帮助我吗!!提前致谢!!
我想将文档导入到我的应用程序中。我创建了一个演示来导入文档。一个演示正在运行。下面是要打开的Demo的代码UIDocumentPickerViewController。
-(IBAction) btnOpenClicked{
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:[self allowedUTIs] inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
[self presentViewController:documentPicker animated:true completion:nil];
}
-(NSArray*)allowedUTIs{
return @[@"public.data",@"public.content",@"public.audiovisual-content",@"public.movie",@"public.audiovisual-content",@"public.video",@"public.audio",@"public.text",@"public.data",@"public.zip-archive",@"com.pkware.zip-archive",@"public.composite-content",@"public.text"];
}
Run Code Online (Sandbox Code Playgroud)
在我的实际项目中实现了相同的代码。UIDocumentPickerViewController 打开并且应用程序能够导入文件,但问题是在实际应用程序中我看不到标题中的任何按钮。以为有动作发生但按钮不可见。请检查演示和实际应用程序的屏幕截图。

我正在使用UIDocumentPickerViewController来选择文档。以下是指定的UTI:
NSArray *types = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeSpreadsheet,(NSString*)kUTTypePresentation,(NSString*)kUTTypePDF,(NSString*)kUTTypeRTF,(NSString*)kUTTypePlainText,(NSString*)kUTTypeText];
UIDocumentPickerViewController *dpvc = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
Run Code Online (Sandbox Code Playgroud)
从页面应用创建的文件(页面文件)显示为灰色,无法选择。但是WhatsApp文档选择器允许选择相同的文件。我是否缺少任何必需的UTI?
我的应用程式:
WhatsApp:
更新
com.apple.iwork.pages.sffpages可以解决我设备上的页面文件,但不适用于icloud驱动器上的文件。呈现文档选择器的完整代码为:
-(IBAction)showDocumentPicker:(id)sender
{
NSArray *types = @[(NSString*)kUTTypeImage,(NSString*)kUTTypeSpreadsheet,(NSString*)kUTTypePresentation,(NSString*)kUTTypePDF,(NSString*)kUTTypeRTF,(NSString*)kUTTypePlainText,(NSString*)kUTTypeText, @"com.apple.iwork.pages.sffpages"];
UIDocumentPickerViewController *dpvc = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
dpvc.delegate = self;
//colorFromHex 4285f4
[[UINavigationBar appearance] setTintColor:[UIColor colorWithRed:66.0/255.0 green:133.0/255.0 blue:244.0/255.0 alpha:1.0]];
[self presentViewController:dpvc animated:YES completion:nil];
}
Run Code Online (Sandbox Code Playgroud) 每当我从我的应用程序中使用 UIDocumentPickerviewcontroller 从 iCloud 中选择文件时,它都会在iPhone模拟器上显示此警报。
NSArray *types = @[(NSString*)kUTTypeArchive];
UIDocumentPickerViewController *docPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:types inMode:UIDocumentPickerModeImport];
docPicker.delegate = self;
docPicker.modalPresentationStyle = UIModalPresentationPopover;
[self presentViewController:docPicker animated:YES completion:nil];
Run Code Online (Sandbox Code Playgroud) simulator objective-c ios icloud uidocumentpickerviewcontroller
我在UIViewController确认以下代码中有此代码UIDocumentPickerDelegate:
- (void)openTextFilePicker {
NSArray *UTIs = [NSArray arrayWithObjects:@"public.text", nil];
[self openFilePicker:UTIs];
}
- (void)openFilePicker:(NSArray *)UTIs {
UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:UTIs inMode:UIDocumentPickerModeImport];
documentPicker.delegate = self;
documentPicker.popoverPresentationController.barButtonItem = self.importButton;
[self presentViewController:documentPicker animated:TRUE completion:nil];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURLs:(NSArray<NSURL *> *)urls {
[self documentPicker:controller didPickDocumentAtURL:[urls firstObject]];
}
- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
NSLog(@"picked document %@", url);
}
- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
NSLog(@"cancelled");
}
Run Code Online (Sandbox Code Playgroud)
这在 iOS 中运行良好。在 Mac Catalyst 中,文件选择器打开,我可以导航和选择一个文件,但是当我单击选择器中的打开按钮时,既不调用didPickDocumentAtURLs也不didPickDocumentAtURL调用。但是,如果我单击选择器中的取消按钮,documentPickerWasCancelled则会调用。 …
我使用 didPickDocumentAt 函数来检索 DocumentPickerViewController 中所选文件的 url。我有一个分段上传功能,它使用 alamofire 将文件上传到后端。
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
print(url)
uploadMultipartFile()
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何获取我选择的文件的数据表示,以便将其传递给我的 uploadMultipartFile 函数?
文件可以是 pdf、.docx 或任何类型的文件。方法是什么?
multipart ios swift alamofire uidocumentpickerviewcontroller
我正在尝试获取带有扩展名的选定文件名,UIDocumentPickerViewController但文件名在文件扩展名的末尾有“]”。关于什么是正确方法的任何建议?
这是我的代码:
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
let filename = URL(fileURLWithPath: String(describing:urls)).lastPathComponent // print: myfile.pdf]
self.pickedFile.append(filename)
// display picked file in a view
self.dismiss(animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 当使用 Xcode 12.5.1 和 IOS 14.5 部署目标遵循 UIDocumentPickerViewController 的现场 IOS 文档时,我收到错误:
Incorrect argument label in call (have 'forOpeningContentTypes:', expected 'forExporting:')
Run Code Online (Sandbox Code Playgroud)
关于现在应该如何使用 IOS UIDocumentPickerViewController 有什么建议吗?
我的代码除了:
// Create a document picker for directories.
let documentPicker =
UIDocumentPickerViewController(forOpeningContentTypes: [.folder])
documentPicker.delegate = self
Run Code Online (Sandbox Code Playgroud)
错误快照:
有关如何使用文档选择器的文档,请参见此处:
https://developer.apple.com/documentation/uikit/view_controllers/providing_access_to_directories给出:
我正在尝试打开一个仅允许某些文件类型的选择器。我正在使用此文档来查找我需要的 UTTYpe: https://developer.apple.com/documentation/uniformtypeidentifiers/uttype/system_declared_uniform_type_identifiers
但我找不到 .pages、.doc、.docx 的 UTTYpe...
我应该怎么办?
这是我目前的代码
func presentPickerDocument() {
//Create a picker specifying file type and mode
var supportedTypes: [UTType]
supportedTypes = [UTType.pdf, UTType.url]
let documentPicker = UIDocumentPickerViewController(forOpeningContentTypes: supportedTypes, asCopy: true)
documentPicker.delegate = self
documentPicker.allowsMultipleSelection = false
present(documentPicker, animated: true, completion: nil)
}
Run Code Online (Sandbox Code Playgroud) 我已经集成UIDocumentPickerDelegate或者UIDocumentMenuDelegate,我在从 URL 或 DocumentPicker 获取文件时遇到问题。如何在 iOS swift 中解决此问题?
我正在尝试从 documentPicker 获取文件类型。我允许我的用户上传 PDF/WORD(doc/docx) 文档类型,对我来说知道是哪一种文档类型至关重要,这样我就可以重命名文件并上传到我的 AWS S3。
感谢任何帮助,我找不到直接从 UIDocumentPickerViewController 获取它的方法。
因此,我试图找到整个 URL 的长度,然后减去 2/3 的长度来找到第一个字母“d”或“p”,然后我可以相应地设置名称。但是我收到此错误,无法使用类型为“(URL)”的参数列表调用类型“String”的初始值设定项。
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let myURL = url as URL
print("import result : \(myURL)")
let s3BucketName = "adnexio-directory/cv_upload"
let url = myURL
var remoteName = ""
//if (url[url.length as Int - 3] == "d")
//if (str[str.count - 3] == "d")
if (url.length - 3 == "d")
{
remoteName = "IOSTEST.docx"
}
else if (url.length - 2 == "d")
{
remoteName …Run Code Online (Sandbox Code Playgroud) uidocumentpickerviewcontroller ×11
ios ×10
swift ×7
objective-c ×3
file ×2
alamofire ×1
icloud ×1
iphone ×1
mac-catalyst ×1
multipart ×1
simulator ×1
swift3 ×1
types ×1
uidocument ×1
url ×1
uttype ×1
xcode ×1