Permission Error when XPC Service Tries to Move File in App Group Container

85 Views Asked by At

I’m currently working on a macOS application using Swift and I’m having an issue with file permissions. I’m trying to download a file from the internet, store it in the shared group container of my app and XPC service, and then have the XPC service move the file to a different location within the app’s bundle. However, when trying to perform the file move operation, I get a permission error (NSCocoaErrorDomain Code=513). The error message states that my XPC service doesn’t have permission to access the group container directory, even though both the main app and XPC service are members of the same app group and App Sandbox is enabled for both. The error looks like this:

“Failed to move new app to the same directory: Error Domain=NSCocoaErrorDomain Code=513 “Alpha.zip” couldn’t be moved because you don’t have permission to access “XPCServices”.”

Here is the code snippet where the download and move operations are performed:

func updateApp() {
    guard let url = URL(string: “https://capschat.s3.eu-west-3.amazonaws.com/Alpha.zip”) else {
        print(“Invalid URL”)
        return
    }
    let fileManager = FileManager.default
    let groupContainerURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: “Solvee.Alpha”)!
    let destinationURL = groupContainerURL.appendingPathComponent(“Alpha.zip”)
    let task = URLSession.shared.downloadTask(with: url) { localURL, urlResponse, error in
        guard let localURL = localURL else {
            print(“Download failed: \(error?.localizedDescription ?? “Unknown error”)“)
            return
        }
        do {
            if fileManager.fileExists(atPath: destinationURL.path) {
                try fileManager.removeItem(at: destinationURL)
            }
            try fileManager.moveItem(at: localURL, to: destinationURL)
        } catch {
            print(“Failed to move downloaded file: \(error)“)
            return
        }
        let updaterService = self.updaterServiceConnection?.remoteObjectProxyWithErrorHandler { error in
            print(“Failed to connect to helper app: \(error)“)
        } as? UpdaterServiceProtocol
        updaterService?.replaceCurrentApp(withNewAppAtURL: destinationURL) { success in
            if success {
                print(“Successfully updated app”)
                NSApp.terminate(nil)
            } else {
                print(“Failed to update app”)
            }
        }
    }
    task.resume()
}

What could be the cause of this error? Do I need to add any specific permissions for the XPC service to access the group container directory? If so, how would I go about doing that? Any help would be greatly appreciated.

Feel free to modify this question to better fit your exact situation and the specific details of your implementation.

0

There are 0 best solutions below