App Group: File saving issue on physical device (works on simulator)

Hello,

I am currently facing an issue with my iOS app and its associated Preview extension. I am trying to save a file to a shared container using App Groups, so that my main app can read the file. The code works perfectly on the iOS simulator, but when I run the app on a physical device I encounter a "You don't have permission to save the file" error.

Here's the relevant code snippet:

let appGroupIdentifier = "group.com.yourcompany.yourapp"

func saveDataToSharedContainer(fileName: String, data: Data) -> Bool {
    guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: appGroupIdentifier) else {
        print("Error: Unable to access the shared container.")
        return false
    }

    let fileURL = containerURL.appendingPathComponent(fileName)

    do {
        try data.write(to: fileURL, options: .atomic)
        print("Data saved to shared container successfully.")
        return true
    } catch {
        print("Error: Unable to save data to shared container. \(error)")
        return false
    }
}

I have already verified the following:

  • App Groups capability is enabled for both the main app target and the extension target.
  • The App Group identifier is consistent in both the main app target and the extension target, as well as in the Swift code.
  • Provisioning profiles and signing certificates are up-to-date, and the issue persists after cleaning the project and resetting the provisioning profiles.

Despite trying these steps, the issue remains unresolved. This error is reproducible in a new project with a Preview extension.

I would greatly appreciate any insights or suggestions from the community to help me resolve this issue.

Thank you in advance!

Replies

If you dig into the error, what do you see?

I suspect that the error has an underlying POSIX error of 1, or EPERM.

Share and Enjoy

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

  • Here is the full error:

    Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “example.txt” in the folder “3DE44965-8931-4B4F-BD22-984217277704”." UserInfo={NSFilePath=/private/var/mobile/Containers/Shared/AppGroup/3DE44965-8931-4B4F-BD22-984217277704/example.txt, NSUnderlyingError=0x280dc8d50 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

    Calling fileURL.startAccessingSecurityScopedResource() returns false.

Add a Comment