I'm trying to persist a bookmark to an external device (mass storage controller connected via camera adapter) across disconnection / reconnection, but it is failing at startAccessingSecurityScopedResource.
The URL is initially retrieved using
UIDocumentPickerViewController *documentProvider;
documentProvider = [[UIDocumentPickerViewController alloc] initForOpeningContentTypes:[NSArray arrayWithObjects:UTTypeFolder, nil]];
documentProvider.delegate = self;
documentProvider.modalPresentationStyle = UIModalPresentationOverFullScreen;
[self presentViewController:documentProvider animated:YES completion:nil];
and then persisted to a bookmark using
DeviceBookmark = [url bookmarkDataWithOptions:NSURLBookmarkCreationMinimalBookmark includingResourceValuesForKeys:nil relativeToURL:nil error:nil]
When accessing the resource I use
NSURL *url = [NSURL URLByResolvingBookmarkData:DeviceBookmark options:NSURLBookmarkResolutionWithoutUI relativeToURL:nil bookmarkDataIsStale:&isStale error:&error]
to retrieve the new URL.
If I don't remove the MSC then the retrieved URL remains the same and functions as expected. If I remove and reconnect the MSC then the URL changes, I get true for isStale and nil for error but startAccessingSecurityScopedResource fails.
I've been banging my head against this for about a day now, but can't see what the issue can be. I've tried adding some related permissions to the entitlements, but this seems to be macOS related as far as I can tell.
What am I missing?!
I found the issue, and it was suitably obscure. The MSC in question is provided by a microcontroller. It just happened in the boot sector (FAT12) the volume serial number was set to 0. While I would think 0 is still a valid number, in this case it doesn't seem to be. Setting it to non-zero allows the bookmark to resolve correctly.
I'm sure no-one else will ever come across this, but there we go, a solution.