Create URL Bookmark for a security-scoped URL

Hi,


I'm using UIDocumentBrowserViewController to browse the files. From the controller, I get the URL of the selected file.

Let's assume that I browse outside my app iCloud container, eg. I go to the 'iCloud Drive/Preview' and select a file located there I cannot access it.


Happens those are not regular files, those are alias files (URL Bookmarks). The bookmarks created by the Preview.app on macOS (10.13.1).


.                                   
..                                  
.00872934-B442-4252-90E6-71DF662D63D0.alias
.0137F126-9988-49F6-BD4E-EB07F434FE81.alias
.0794E1B9-36A4-4181-B41A-629864D9C8E4.alias
.0835FFAA-C6B1-44B7-B263-9B3D091DF85E.alias
.092CC36B-657B-47E1-829C-741AD19BF80E.alias
.0F2518EF-EB0D-42CE-BEFA-DCD309B4BE6E.alias
.1358DBE1-9B48-461A-956F-8C651656D955.alias
.178FF63B-4260-438F-92D3-4C9C8AE4F050.alias
.17FAE9F2-BE54-4143-952B-10A03018DD1B.alias
.19610591-C1A3-4403-B765-DCB7CB59532A.alias
.1EAECE6A-56E1-4367-A0EF-5A5A2521B35A.alias


In my case, the bookmark file points to another directory (that is an iCloud Drive directory):

.00872934-B442-4252-90E6-71DF662D63D0.alias -> file:///private/var/mobile/Library/Mobile%20Documents/com~apple~CloudDocs/Desktop/file.pdf

Since I know that I selected a bookmark file, I try to resolve the bookmark and access the destination file "Desktop/file.pds". To do so I have to as for the security-scoped access, like this:


selectedFileURL.startAccessingSecurityScopedResource() // true



then I want to ask for the access to the parent directory

selectedFileURL.deletingLastPathComponent?.startAccessingSecurityScopedResource() // false


but in that case, the access is not granted. It is not always the case though. For the application iCloud container directory, the access is granted.


Question:

1. Is this expected behavior?

Replies

Yes, that's the correct behavior. The security-scoped URL gives you access only to the item it represents. If you could "trick" the security mechanism by changing the URL, there would in effect be no security.


In general, if you have a security-scoped URL to a directory (though you don't in this case), you can access any file or subdirectory within it. But starting from a file, you can't get to any directories.