0

My app has the following targets:

  • iOS App
  • Today Extension
  • Apple Watch App
  • Apple Watch Extension

All of these targets have the "App Group" capability with the correct group selected. In the main iOS app, I synchronize the settings like so:

NSUserDefaults * sharedDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.foo.bar"];
if([sharedDefaults respondsToSelector:@selector(setObject:forKey:)]){
    // Example Value
    [sharedDefaults setObject:@"TestString" forKey:@"TestString"];
    [sharedDefaults synchronize];
}

The Today Extension uses the following to get the settings, which works just fine:

NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.foo.bar"];
NSLog(@"String: %@", [defaults stringForKey:@"TestString"]);
// String: TestString

The watch extension uses the same line of code to get the settings as the today extension, however it does not work. In the console I see String: (null). What am I doing incorrectly?

ecnepsnai
  • 1,882
  • 4
  • 28
  • 56

2 Answers2

0

Did you create your watch target as a watchOS WatchKit Application (watchOS 2) or as an iOS Application Extension (watchOS 1)? If targeting for watchOS 2, according to the transition guide: You cannot rely on a shared group container to exchange files with your iOS app. Fetching files involves transferring them wirelessly to Apple Watch.

Keller
  • 17,051
  • 8
  • 55
  • 72
0

Please refer to settings bundle not working on watchOS 2 Apparently the docs are not that clear on what can and cannot be done with Preferences, the only way I found was to ensure that I copy the required user defaults over and keep them synced manually (NSUserDefaults not working on Xcode beta with Watch OS2).

Community
  • 1
  • 1
insanoid
  • 413
  • 2
  • 10