How to disable Documents folder protection

catalinafinderpermissionxcode

I'm working on a C++ program in Xcode, which opens some files in the project folder, which is in ~/Documents/Xcode Projects. Because it's in my Documents folder, every time I run the program, it asks for permissions to access my documents folder, and I have to click yes every time. I tried explicitly giving the program permission in the settings, and that works for a little bit, but after about 3 runs of the program it asks for permissions again. Can I just disable the document folder protection? How?

Best Answer

The easy way is to turn off sandboxing for the app, in Xcode project settings under "capabilities". You might not want that though.

Otherwise you probably need a security-scoped bookmark to the directory. When you get access, create a bookmark from the directory URL using bookmarkData(options:includingResourceValuesForKeys:relativeTo:). Make sure to use .withSecurityScope in the options argument. Save that to user defaults. Then when you want to use the directory,

  • Read the bookmark from user defaults
  • Initialize the URL using init(resolvingBookmarkData:options:relativeTo:bookmarkDataIsStale:) with options of [.withSecurityScope, .withoutUI].
  • Call .startAccessingSecurityScopedResource() before trying to use the URL.
  • Call .stopAccessingSecurityScopedResource() when you're done with the URL.