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!

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"

Hi I am experiencing the same issue with a QLPreview - writing works on Simulator does not on Device. Did you find a solutions? Thanks

I’m going to ask you the same question I asked martianrover.

IMPORTANT Please reply as a reply, not in the comments. I’m not notified of the latter, which is why I didn’t see martianrover’s reply )-:

Share and Enjoy

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

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