Permission to read a local file

I have a question about the file system of macOS and iOS. I tried to play an audio from the local download folder. But I failed because my application seems not to have the permission to read it. I get the error Error Domain=NSOSStatusErrorDomain Code=-54 "permErr: permissions error (on file open)". So I think it's something different from other systems like Windows. How are these permissions handled and how can I get the permission to read a file? I read something about Sandboxing but my app doesn't even start when I turn on "App Sandbox" in the settings.

Are you making an app for yourself or for other people to use?

If you are making an app for other people and want to play an audio file, you should bundle the file with the app and load and play the file from the app bundle. Using the app bundle is better than relying on a file being in the Downloads folder.

macOS has privacy preferences in the Security & Privacy section of its Settings/System Preferences app. You can give an app full disk access or give an app access to specific folders. If you are making an app for yourself, use these settings to give your app permission to access what you need on the file system.

You can give a sandboxed app access to the Downloads folder from the File Access section of the App Sandbox settings in Xcode.

What szymczyk said plus…

If the file was downloaded to the user’s Downloads folder, it’s likely you’re hitting a MAC restriction. See On File System Permissions for more background on this.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks a lot for the answers, @szymczyk and @eskimo. I got a better understanding of the system and its privileges system. But for some reason I can't give my test-app full privileges. I will tell you in detail what I did:

I gave my test-app full access to disk. This was not possible immediately after compiling but after a short time. I found out that when the .XCInstall folder appeared in the Release- or Debug-folder, I was able to add my test-app in the "full access to disk"-list in the system settings. Can I enforce that the .XCInstall-folder will be created? Because the .app-files in there can be executed by my Mac without running Xcode. It seems that these files are portable and could be added to another Mac (by USB-stick for example) and can then be executed (after updating the system-settings...), equally to the .exe-files on Windows. I have to add that I only want to create apps for private use. So I also don't want to buy an Apple Developer Enrollment. And there is the problem with how to force Xcode to export the .app-file that can be executed directly by a Mac when starting the .app in the Finder or desktop.

I added these lines of code to my test-app:

  1. Get the url:let url = URL(filePath: "/Users/myname/Desktop/Musik/title.mp3")
  2. Call the constructor of ACAudioPlayer with the needed arguments: player = try AVAudioPlayer(contentsOf: url, fileTypeHint: AVFileType.mp3.rawValue)

3.player.play()

In the iPhone simulator this works fine. The mp3 file is played. But as soon as I switch over to my Mac there is this error "The operation couldn’t be completed. (OSStatus error -54.)" which seems to be the permissions error.

To create an app you can copy to other Macs, create an archive of your project and export it. In Xcode choose Product > Archive to create the archive.

After creating the archive you can access it from Xcode's Organizer window. Choose Window > Organizer in Xcode to open the Organizer window.

Select Archives from the left side of the Organizer to show the archives. You may have to choose your project from a menu above the Archives item on the left side of the window. Select your archive from the list and click the Distribute App button.

A sheet opens asking for a method of distribution. Select Copy App because you do not have a paid developer account. Click the Next button. Choose a location to save the archived app and click the Export button. Now you have an app file you can run like any other app on your Mac and copy to other Macs.

Permission to read a local file
 
 
Q