UIDocumentPickerViewController 未在 Mac Catalyst 上调用 didPickDocumentAtURL

UIDocumentPickerViewController not calling didPickDocumentAtURL on Mac Catalyst

我在 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");
}

这在 iOS 中工作正常。在 Mac Catalyst 中,文件选择器打开,我可以导航和 select 一个文件,但是当我点击选择器中的打开按钮时,既没有调用 didPickDocumentAtURLs 也没有调用 didPickDocumentAtURL .但是,如果我单击选择器中的取消按钮,则会调用 documentPickerWasCancelled

当我单击“打开”时,控制台中出现此错误:

Failed to associate thumbnails for picked URL file:///**** with the Inbox copy file:///****: Error Domain=QLThumbnailErrorDomain Code=102 "(null)" UserInfo={NSUnderlyingError=0x600000da9a10 {Error Domain=GSLibraryErrorDomain Code=7 "no storage for file:///****" UserInfo={NSDescription=no storage for file:///****}}}

我认为这可能意味着我的沙箱安全设置有问题,但是当我将选择器模式更改为 UIDocumentPickerModeOpen 时,该错误不再发生,但仍未调用 didPickDocumentAtURL .不管怎样,这是我的沙箱设置:

我已经看到 post 关于文档选择器打开空白的信息,但这不是这里的问题。我只看到一个 post 似乎证实它确实有效,但它使用的是 Swift ... 它是否可以在 Swift 中工作但在 Objective C 中损坏?我还缺少其他东西来完成这项工作吗?

更新

这里是 a new Xcode project 用最少的代码来演示问题。与我的完整项目一样,这在 iOS 上运行良好,但不会在 Mac.

上调用 didPickDocument... 委托方法

据我所知,您刚刚在方法签名中打错了字,正确的是

- (void)documentPicker:(UIDocumentPickerViewController *)controller 
didPickDocumentsAtURLs:(NSArray <NSURL *>*)urls {
    NSLog(@"picked URLs %@", urls);
    // selecting multiple documents is cool, but requires iOS 11
    [self documentPicker:controller didPickDocumentAtURL:[urls firstObject]];
}

在 iOS 上有效,因为仍然支持已弃用的旧版本

- (void)documentPicker:(UIDocumentPickerViewController *)controller 
  didPickDocumentAtURL:(NSURL *)url; 

但 macOS/Catalyst 没有,因此您的委托由于缺少方法而未被调用。

经过测试并适用于 Xcode 11.2

backup

Swift 适用于 Catalyst 的新 API 版本是:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    // handle urls
}