UIDocumentPickerViewController ignores directoryURL on iOS/iPadOS 16.1

I found a glitch on my app on iOS/iPadOS 16.1. The directory to open files is not saved. I filed this on Feedback Assistant but Apple says that it is a specified behavior. Isn't it a bug?

//
//  ViewController.m
//

#import "ViewController.h"
#import <UniformTypeIdentifiers/UniformTypeIdentifiers.h>

@interface ViewController () <UIDocumentPickerDelegate>

@end

@implementation ViewController
{
    NSURL *directoryURL;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)documentPicker:(UIDocumentPickerViewController *)controller didPickDocumentsAtURLs:(NSArray<NSURL *> *)urls
{
    directoryURL = [[urls firstObject] URLByDeletingLastPathComponent];
}

- (IBAction)trigger:(id)sender
{
    UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:@[UTTypeData] asCopy:NO];
    picker.directoryURL = directoryURL;
    [self presentViewController:picker animated:YES completion:nil];
}

@end

To reproduce the issue.

  1. Tap the button and select a file on another directory.
  2. Tap the button again.
  3. The directory should be the selected one but is the default one.

I forgot setting delegate. but the issue still occurs.

- (IBAction)trigger:(id)sender
{
    UIDocumentPickerViewController *picker = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:@[UTTypeData] asCopy:NO];
    picker.delegate = self;
    picker.directoryURL = directoryURL;
    [self presentViewController:picker animated:YES completion:nil];
}

Unfortunately, I also realised that directoryURL has no functionality. The user should be able to load a file from one directory and save the edited file in another directory. I would like to set the two paths as directoryURL depending on the load or save function. This way the user would not have to keep changing directories if he wants to save the edited file in a different directory than the original file. If the directory keeps changing to the last one used, the user will quickly become annoyed.

Maybe someone else has found a solution?

In the method "documentPicker:didPickDocumentsAtURLs:" I get the path in this form: "/private/var/mobile/Containers/Shared/AppGroup/529E1B6C-A2B3-47E3-3623-7D5AC5F11BBC/File Provider Storage/Downloads/"

I try to assign this path as a directoryURL when calling a UIDocumentPickerViewController again. That does not work. The UIDocumentPickerViewController opens the last used path and not "Downloads". Maybe I have to change the path somehow to make it work? No longer “Shared/AppGroup/” but direct path to the “Files” app. But how do I have to change the path?

Ok, now I understood how directoryURL from the UIDocumentPickerViewController works. It expects a security-scoped URL. Either a direct "Security Scoped URL" or from a "Security-Scoped Bookmark". This means that the user must first select the folders that are to be available using the UIDocumentPickerViewController with directoryURL. The returned security-scoped URLs can be saved as security-scoped bookmarks and later one can be passed to the UIDocumentPickerViewController as directoryURL.

It is important that before you create a bookmark with bookmarkDataWithOptions, you must call startAccessingSecurityScopedResource from the url. And after creating the bookmark, call stopAccessingSecurityScopedResource.

UIDocumentPickerViewController ignores directoryURL on iOS/iPadOS 16.1
 
 
Q