In my app I need to have access to the users desktop, and I would like to implement the standard dialogue for the user to give permission for this access at launch. I do not want to use the NSOpenPanel() for the user to select the desktop, as I dont think that is an elegant solution.
However I am having issues implementing this.
I use the following code to be granted access to the Desktop URL:
let accessGranted = desktopURL.startAccessingSecurityScopedResource()
However no dialogue box appears and the call returns false
I have also included "Desktop Usage Description" in my plist.
Here is my code
@State var message:String = "Good Luck!" var body: some View { VStack { Button("Get Desktop files") { accessDesktopWithPermission() } Text(message) } .padding() } //: –—–—–—–—–—–—–—–—–—–—–—–—–—–— :// func accessDesktopWithPermission(){ guard let desktopURL = getDesktopURL() else{ return } let accessGranted = desktopURL.startAccessingSecurityScopedResource() if accessGranted{ if let content = try? FileManager.default.contentsOfDirectory(at: desktopURL, includingPropertiesForKeys: nil ){ message = "Found \(content.count) on Desktop" } else{ message = "issue loading file from desktop" } } else{ message = "Access denied to:\(desktopURL )" } }
obviously I have setup something incorrectly so I have also attached my code if anyone is interested to take a look.
[https://www.openscreen.co/DesktopAccess.zip)
I use the following code to be granted access to the Desktop URL:
let accessGranted = desktopURL.startAccessingSecurityScopedResource()
I think you've misunderstood what "startAccessingSecurityScopedResource" actually does. The documentation describes this in more detail, but that API is used when you want to start using a security scoped URL. It does NOT provide direct access and will only work when you already have a security scoped URL.
That leads to here:
I have also included "Desktop Usage Description" in my plist.
That key (and similar ones) aren't actually relevant to a sandboxed app. If you look at the documentation for NSDesktopFolderUsageDescription, it described two kinds of access.
-
The user granted the user access to that location (for example, through an open panel).
-
The app directly accessed a location without any user involvement/consent.
It then says:
"The first time your app tries to access a file in the user’s Desktop folder without implied user consent, the system prompts the user for permission to access the folder’s contents."
In other words, the system will present that dialog when your app tries to go through the second access type above.
However, the key point for a sandboxed app is that it then later says:
"App Sandbox enforces stricter limits on Desktop folder access, so that policy may supersede this one if your app enables sandboxing. See App Sandbox for more information."
What isn't clear from that languages is that for most user directories (including "Desktop"), the sandbox's policy is very simple. It ONLY allow access the user has granted (#1 above). That's why the system never presents your dialog- the sandbox denied access to "Desktop" before the system would have presented the dialog. Similarly, if you turn off the App Sandbox it will immediately start showing the permission dialog.
__
Kevin Elliott
DTS Engineer, CoreOS/Hardware