I want to create a simple iOS app that syncs (and shares some data) with a watch app. I've tried to do this using AppGroup and UserDefaults. iOS app saves the data but the Apple Watch app can't read it. Does anyone know the reason? Can it be because of my free Development Account?
iOS code:
@IBAction func buttonPressed(_ sender: Any) {
print(FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.my.app")!
.appendingPathComponent("switchState.plist"))
var sharedDefault = UserDefaults(suiteName: "group.com.my.app")
sharedDefault?.set(textFieldOne.text, forKey: "shared1")
print(UserDefaults(suiteName: "group.com.my.app")!.string(forKey: "shared1")!)
sharedDefault?.synchronize()
}
iWatch code:
@IBAction func buttonPressed() {
let sharedDefault = UserDefaults(suiteName: "group.com.my.app")
print(FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.my.app")!
.appendingPathComponent("switchState.plist"))
print(UserDefaults(suiteName: "group.com.my.app")?.object(forKey: "shared1"))
let sharedText = sharedDefault!.string(forKey: "shared1")
textField.setText(sharedText)
print(sharedText)
}
when I run iOS app - it writes to switchState.plist a key with value in a path: /Users/_userName/Library/Developer/CoreSimulator/Devices/F5A0485A-CBB7-4903-8339-738DBE3491B6/data/Containers/Shared/AppGroup/5C371E82-27A7-4BB3-982D-B0E5CDE5AC52/Library/Preferences/switchState.plist
but when I try to read that data in iWatch app -
/Users/_userName/Library/Developer/CoreSimulator/Devices/6F195AF7-73CA-4AF9-8CFE-5B47286F3DE5/data/Containers/Shared/AppGroup/533FEFB3-4A77-4A2B-8A53-99CF916D5481/
there is no plist file (return nil, of course)!
BUT! When I manually adds Library/Preferences/switchState.plist in second path - it works perfectly!
I've tried this on real devices - iWatch app says:
file:///private/var/mobile/Containers/Shared/AppGroup/D297CDB8-F623-4BDF-83D8-15505451EC92/switchState.plist 2020-03-04 10:59:25.736335+0200 IosWatchOsTest Extension[288:94115] [User Defaults] Couldn't read values in CFPrefsPlistSource<0x16d92960> (Domain: group.com.sk.todoey-ios13.Todoey, User: kCFPreferencesAnyUser, ByHost: Yes, Container: (null), Contents Need Refresh: Yes): Using kCFPreferencesAnyUser with a container is only allowed for System Containers, detaching from cfprefsd
Can you help me with this?