2

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.

matee
  • 91
  • 1
  • 10

2 Answers2

0

In Watch OS1 this worked, but in Watch OS2 there has been some changes. You need to use something called WatchConnectivity to send the data you want to save to the watch. so even if you use shared user defaults or app groups the data you put inside them will be on the watch and so not accessible from the iOS app.

To send data to the watch you can use the WatchConnectivity framework.

Ashish Sahu
  • 388
  • 3
  • 16
  • Hi, Thanks for your reply, I have just updated the git source code by using the NSFileMananger to store the data I would like to share in the shared path, but still doesn't work as I expect. Is there anything I missed or it has been banned by Apple(didn't see any updates from official so I assume it should work). by doing so, watch extension is able to load the data without trigging the main app. If I change to use connectivity sessions, it will need to open the app/or keep it in background when watch fetches the data. – matee Feb 16 '16 at 13:05
0

Thanks Ashish to bring the difference between OS1 and OS2 up.

just found a good reference from: NSUserDefaults(suiteName:) on iOS 9 and WatchOS 2 - not working?

In watchOS 2 you need to keep in mind that there is 2 different processes running:

Apple Watch Process iPhone Process Both of these processes have their own sandbox that's why they call it "native", so if you try to use the shared NSUserDefaults it will not work because the Apple watch app has a completely different sandbox than the host iPhone app.

If you want to save something from your phone to the NSUserDefaults on the Apple watch Target:

Use WatchConnectivity to send the data you want to save to the watch. Then when the watch receives the data you sent to it, save it to the Apple watch's default NSUserDefaults.

Community
  • 1
  • 1
matee
  • 91
  • 1
  • 10