Cannot get contentsOfDirectory on AFP volume (MacOS)

I am trying to use FileManager contentsOfDirectory(at:,includingPropertiesForKeys:,options:) to get the contents of the root level of an AFP mounted volume. The call throws an exception. Volume is mounted and readily accessible in Finder.
The url content is "file:///Volumes/incoming/"
The error received is The file “incoming” couldn’t be opened because you don’t have permission to view it.
Here is the relevant code:


        do {
            let contents = try FileManager.default.contentsOfDirectory(at: url, includingPropertiesForKeys: nil, options: [.skipsHiddenFiles,.skipsPackageDescendants,.skipsSubdirectoryDescendants])
            result = contents
        }
        catch let error as NSError {
            print(error.localizedDescription)
        }

Is there something else I need to be doing? I have not done MacOS dev for a while so I am guessing I am missing something simple. Some setting in the info.plist required now or something along those lines?

Accepted Reply

The sandbox does not prevent you from accessing files on a volume, it prevents you from accessing files without the user’s explicit consent. The user gives that consent by either choosing the file in one of the standard open or save dialogs, or by dragging it into your app. If you want to be compatible with the Mac App Store, you have to structure your app to use one of those techniques. For example, based on the volume name (

Incoming
) it looks like your app is monitoring a volume for activity, kinda like a dropbox. In that case your best option is to restructure your UI so that you don’t hard code the dropbox, but instead ask the user to choose it using a standard open dialog. Once the user has chosen it, you will then have access to it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Replies

I found it. App Sandboxing is the issue. Turning that off allowed me access. But if I understand correctly, if I wanted my app to be available in the Mac App Store, it must be sandboxed. Is there any middle ground here? The app's purpose requires full access to the file system (read and potentially delete).

The sandbox does not prevent you from accessing files on a volume, it prevents you from accessing files without the user’s explicit consent. The user gives that consent by either choosing the file in one of the standard open or save dialogs, or by dragging it into your app. If you want to be compatible with the Mac App Store, you have to structure your app to use one of those techniques. For example, based on the volume name (

Incoming
) it looks like your app is monitoring a volume for activity, kinda like a dropbox. In that case your best option is to restructure your UI so that you don’t hard code the dropbox, but instead ask the user to choose it using a standard open dialog. Once the user has chosen it, you will then have access to it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Ah, Quinn, we have been at this too long. I remember you helping me out with some issues on a IIgs app! Thanks for the clarification. Right now the app is just a personal tool so its fine, but good to know how it will need to change if I try to turn it into a product.
Thanks!