just started swifting recently and got an issue by using app group to share data between iOS devices.
basically I have setup the project followed the steps below:
[iPhone]
- Enabled App Group for the iPhone target
initialed data below(groupID is matched what I set in the project):
sharedDefaults = NSUserDefaults.init(suiteName: groupID) sharedDefaults?.setObject("User Default Shared String", forKey: "test") sharedDefaults?.synchronize()
double checked the test string by loading user default locally, which is able to display in the Log.
let t_sharedDefaults = NSUserDefaults.init(suiteName: groupID); t_sharedDefaults?.synchronize(); let str = t_sharedDefaults?.valueForKey("test") as! String; print(str);
[watch extension]
- Create a new watch extension target.
- Enable App Group.
Load the User default data under awake with context via the code below:
sharedDefaults = NSUserDefaults.init(suiteName: groupID); sharedDefaults?.synchronize(); let str = sharedDefaults?.valueForKey("test") print(str);
I have run the iPhone target first, and then the watch app. However, the watch app is not able to read expected data from the user default.
I have also uploaded the test project in Github, please let me know if you have any thought on this issue.
[Github]
https://github.com/mattcn/WatchOS_DataSharing
Thanks for your help in advance.