Read file with System Network Extension from App Group

I have trouble with reading a file from an App Group with my System Network Extension.

The app group container is found successfully.

However the file read returns empty.

In the app itself the same code runs fine and returns a string array of items found in the file.


Code:
Code Block
func readFile() -> [String] {
        var jsonResult: [String] = []
        guard let containerURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: AppConstants.groupID) else {
            fatalError()
        }
        let fileURL = containerURL.appendingPathComponent("file.json")
        if let data = try? NSData(contentsOfFile: fileURL.path, options: .mappedIfSafe) as Data {
            if let json = try? JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) {
                jsonResult = json as! [String]
            }
        }
        os_log("jsonResult: %{public}@", jsonResult)
        return jsonResult
    }


Log:
Code Block
default 09:42:19.486793+0200 app-network-extension container_create_or_lookup_app_group_path_by_app_group_
identifier: success
default 09:42:20.105792+0200 app-network-extension
jsonResult: ( )


Edit, after more digging:

fileURL is different!

App: file:///Users/me/Library/Group%20Containers/
SysExt: file:///private/var/root/Library/Group%20Containers/
Answered by DTS Engineer in 673355022

I have trouble with reading a file from an App Group with my System
Network Extension.

App Groups won’t help in this scenario because App Group containers are per-user and are the app and the sysex run as different users (the logged in GUI user and root, respectively). The best way to share state between these two components is via IPC (and specifically XPC).

Share and Enjoy

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

I have trouble with reading a file from an App Group with my System
Network Extension.

App Groups won’t help in this scenario because App Group containers are per-user and are the app and the sysex run as different users (the logged in GUI user and root, respectively). The best way to share state between these two components is via IPC (and specifically XPC).

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
....
Read file with System Network Extension from App Group
 
 
Q