1

I'm trying to use UIDocumentPickerViewController. It works fine on the simulator and also on a real device in debug mode. I've tried replacing didPickDocumentAtURL with didPickDocumentsAtURLs but it still doesn't work. I've tried to output some statements inside the method but it doesn't appear as expected.

Here is the smallest code that can reproduce the issue. I have a main view with only one button and button tap handler is defined below.

#import "ViewController.h"

@interface ViewController ()

@end
@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls {
    [Toast show:[NSString stringWithFormat:@"select file's url is %@", [urls firstObject]]]; // not work
}

/* - (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentAtURL:(NSURL *)url {
} */

- (void)documentPickerWasCancelled:(UIDocumentPickerViewController *)controller {
}

- (IBAction)buttonTapped:(id)sender {
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[@"public.data"] inMode:UIDocumentPickerModeImport];

    documentPicker.delegate = self;
    [self presentViewController:documentPicker animated:YES completion:nil];
}

@end

When running on a real device in a real environment, an error will be output:

Tried to call delegate -documentBrowser:didPickDocumentURLs: with an empty array of items. This indicates the items failed to be prepared and materialized on disk: <private>

0

1 Answer 1

1

The problem is due to using a deprecated API.

Answer source: https://developer.apple.com/forums/thread/713814

if (@available(iOS 14.0, *)) {
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:@[UTTypeData] asCopy:YES];
    documentPicker.delegate = self;
    [self presentViewController:documentPicker animated:YES completion:nil];
} else {
    UIDocumentPickerViewController *documentPicker = [[UIDocumentPickerViewController alloc] initWithDocumentTypes:@[(NSString *)kUTTypeData, (NSString *)kUTTypeText] inMode:UIDocumentPickerModeOpen];
    documentPicker.delegate = self;
    [self presentViewController:documentPicker animated:YES completion:nil];
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.