Macos App Sandboxing - Read Access to Referenced Files from Parsed Xml

MacOS App Sandboxing - read access to referenced files from parsed xml

You didn't specify details about the paths you are collecting. Are they relative to the XML document or are they always referring to files in a certain folder?

If it is guaranteed, that the paths you are collecting always point to a common System folder (e.g. "Movies" or "Music"), you can add an entitlement that allows you to gain read/write access to those folders.

If the files are scattered all over the file system, you could prompt your users to select a common root folder via NSOpenPanel and store an app-scoped Bookmark to persist access to the selected folder across app sessions.

OS X App Sandboxing and arbitrary files access - Update to Document-based?

You don't have to convert your app to be document-based to gain access to user selected files and security scoped bookmarks.

I can think of 2 reasons why your current code does not work in a sandboxed environment:

  • You don't have the "User Selected File Access" capability set (Xcode > target > Capabilities > App Sandbox > File Access)
  • You are using the path/NSString based API of the directory enumerator instead of the URL NSURL based one.

A vanilla Xcode project with Sandboxing enabled and the User selected files capabilities set, should enumerate any path obtained via NSOpenPanel:

NSOpenPanel* panel =[NSOpenPanel openPanel];
panel.canChooseDirectories = YES;
[panel beginSheetModalForWindow:self.view.window completionHandler:^(NSInteger result) {
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSURL *directoryURL = panel.URL;
NSDirectoryEnumerator *enumerator = [fileManager
enumeratorAtURL:directoryURL
includingPropertiesForKeys:nil
options:0
errorHandler:nil];
for (NSURL *url in enumerator) {
NSLog(@"url:%@", url);
}
}];

If you want to store the ability to access specific folders from the sandbox across app launch/quit cycles, you will need to store a security scoped bookmark.
This post contains information persisting user selected file/directory access via app scoped bookmark:
Trouble creating Security-Scoped Bookmark

Sanboxed app, include files by reference?

When the file is first dropped you need to create and save a security scoped bookmark which you can the use on a subsequent app launch to regain access to the file. Read Security-Scoped Bookmarks and Persistent Resource Access in Apple's App Sandbox in Depth.



Related Topics



Leave a reply



Submit