iosobjective-cmac-catalystuidocumentpickerviewcontroller

UIDocumentPickerViewController not calling didPickDocumentAtURL on Mac Catalyst


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.


Solution

  • 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