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
Realm Write Transaction Failing, Despite Being in Transaction
Swiftsupport Folder Not Included in The IPA When Generating Build from Script
Why Does a Simple Swift Arithmetic Operation Compile So Slow
Calling Consecutive Animations in Swift with Completion Handler
How to Specify The Name of The Output Executable
How to Implement Applescript Support in a Swift Macos App
Adding Drop Shadow to Table View Cell
How to Change an Inout Parameter from Within a Escaping Closure
Animate UIlabel Width with Fixed Center
Wrong Image Orientation After Displaytransform Call
Error Combining Nscalendarunit with or (Pipe) in Swift 2.0
Is There an Firebase Realtime Database Equivalent to The Increment Method Available in Firestore
How Do Uniqueness Constraints as (Comma,Separated,Attributes) Work with Swift in Coredata
Swift 4: How to Asynchronously Use Urlsessiondatatask But Have The Requests Be in a Timed Queue
How to Pass Data from Using Post/Form Leaf Template