UIDocumentPickerViewController not calling didPickDocumentAtURL on Mac Catalyst
Asked Answered
U

2

5

I have this code in a UIViewController that confirms to 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");
}

This works fine in iOS. In Mac Catalyst, the file picker opens, I can navigate and select a file, but when I click the Open button in the picker, neither didPickDocumentAtURLs nor didPickDocumentAtURL are called. However, if I click the Cancel button in the picker, documentPickerWasCancelled is called.

When I click Open, this error appears in the console:

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:///****}}}

I thought that might mean I had a problem with my sandbox security settings, but when I change the picker mode to UIDocumentPickerModeOpen, that error no longer occurs but didPickDocumentAtURL still isn't called. Anyway, here are my sandbox settings:

sandbox settings with User Selected File set to Read/Write

I've seen posts about the document picker opening blank, but that's not the problem here. I've only seen one post that seems to confirm it does work, but that was using Swift ... could it be working in Swift but broken in Objective C? Is there something else I'm missing to make this work?

UPDATE

Here's a new Xcode project with the minimum code to demonstrate the problem. As with my full project, this works fine on iOS, but doesn't call the didPickDocument... delegate methods on Mac.

Unitive answered 4/3, 2020 at 2:4 Comment(2)
So the actual QLThumbnailErrorDomain message has nothing to do with the problem stated, right?Undertaker
@Undertaker I think that's right.Unitive
U
9

As far as I see you've just made typo in method signature, the correct is

- (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]];
}

on iOS worked because deprecated legacy one still supported

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

but macOS/Catalyst does not, so your delegate just not called due to absent method.

Tested and works with Xcode 11.2

Uird answered 6/3, 2020 at 17:9 Comment(1)
Oh my, you're right! I made "URL" plural in the new method name, but not "Document." Thank you for spotting the error and confirming the solution. I'll award the bounty when that option becomes available.Unitive
G
2

Swift version of new API that works on Catalyst is:

func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
    // handle urls
}
Gorged answered 4/2, 2022 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.