0

I am trying to use NSUserDefaults to access an object I save in my NSUserDefaults.

I have looked everywhere but I must be missing something. Hoping anyone can help me out.

Heres what I have done.

  • I use Xcode 7.1
  • My bundle identifier is "com.mycompany.myapp"
  • I added a watchkit app (new target)
  • Bundle identifier for watch target is "com.mycompany.myapp.watchkitapp"
  • Bundle identifier for extension target is "com.mycompany.myapp.watchkitapp.watchkitextension"
  • I enabled App groups in all 3 targets.
  • App Group ID for all 3 targets is: "group.com.mycompany.myapp"
  • All 3 steps are checkmarked in the App Groups section

I use NSUserDefaults.standardUserDefaults() in all cases but the one where I need the object to be accessible from the watch. In this case I use:

if let userDefaults = NSUserDefaults.init(suiteName: "group.com.mycompany.myapp")
{
    userDefaults.setObject(myDate, forKey: UserDefaults.DateKey.rawValue)
    userDefaults.synchronize()
}

I can reload this data using this code:

let mydefaults = NSUserDefaults(suiteName: "group.com.mycompany.myapp")
mydefaults!.synchronize()
let myDict = mydefaults!.dictionaryRepresentation()
for entry in myDict
{
    print("ENTRY: \(entry)")
}

Works just fine, but executing the same code in awakeWithContext in my watch extensions InterfaceController just prints a few of the default keys. Not the one I set myself.

I basically just need to access a date stored in the iphone app.

Any idea what I am doing wrong?

Stefan
  • 5,203
  • 8
  • 27
  • 51
Lyck
  • 691
  • 1
  • 6
  • 18

1 Answers1

0

You can no longer use app groups sharing with watchOS 2. At least I haven't found any solution yet. It is my understanding that you now have to use WatchConnectivity.

https://forums.developer.apple.com/thread/3927

atlwx
  • 1,334
  • 1
  • 14
  • 9
  • I am only sharing data from the iOS app to the watch app. The watch app is not updating any data. – Lyck Dec 01 '15 at 18:28
  • https://developer.apple.com/library/prerelease/ios/documentation/General/Conceptual/WatchKitProgrammingGuide/DesigningaWatchKitApp.html#//apple_ref/doc/uid/TP40014969-CH3-SW1 "In watchOS 2, iOS automatically forwards a read-only copy of your iOS app’s preferences to Apple Watch. Your WatchKit extension can read those preferences using an NSUserDefaults object, but it cannot make changes directly to the defaults database. In watchOS 1, you can share preferences between your iOS app and WatchKit extension by initializing an NSUserDefaults object with the name of a shared app group." – Lyck Dec 01 '15 at 18:33
  • Oh wow. I didn't see that bit. (oh and if anyone points out that is "prerelease", I double checked, it is also in the current documentation as well). hmmm. I too haven't been able to get the changes to show on the watch. So I ultimately went the watch connectivity route. So I'm now curious too, what are both doing wrong!? – atlwx Dec 01 '15 at 20:51
  • I believe this is only possible to read during init of watchos app. Ie the preferences are not pushed after init. However, I have noticed some unconsistent behaviour when testing on actual device vs the simulator. Ie I did get updated data to the watch. But now Im going the wc session route as well.. – zeAttle Dec 26 '15 at 15:13