2

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.

{Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

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
    }
}

Called with this:

    let fileName = "example.txt"
    let dataToSave = "Hello, World!".data(using: .utf8)!

    if testSaveDataToSharedContainer(fileName: fileName, data: dataToSave) {
        print("Data saved successfully.")
    } else {
        print("Error saving data.")
    }

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!

pmark
  • 281
  • 2
  • 9

1 Answers1

0

It seems you cannot do this, but there is no documentation about the restriction. The topic has come up on the Apple developer forum.

So your only recourse is to file a bug with Apple asking for clarification of the situation.

Faisal Memon
  • 2,711
  • 16
  • 30