Search code examples
iosobjective-cnsuserdefaultsapple-watch

Can't sync NSUserDefaults with apple Watch


I need to pass 2 doubles to apple watch, for this purposes I set app groups in extension and main app, next:

main app:

groupDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.XXXXX"];
[groupDefaults setDouble:pitchDelta forKey:@"pitch_delta"];
[groupDefaults setDouble:rollDelta forKey:@"roll_delta"];
[groupDefaults synchronize];

extension:

groupDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.XXXXX"];
double pitchDelta = [groupDefaults doubleForKey:@"pitch_delta"];
double rollDelta = [groupDefaults doubleForKey:@"roll_delta"];

But the values are not passed. Did I forget something?

in main app code

[groupDefaults setDouble:pitchDelta forKey:@"pitch_delta"];
[groupDefaults setDouble:rollDelta forKey:@"roll_delta"];
[groupDefaults synchronize];
        NSLog(@"%f %f", [groupDefaults doubleForKey:@"pitch_delta"], [groupDefaults doubleForKey:@"roll_delta"]);

returns in log -0.387812 -0.098052


Solution

  • Try deleting the apps entirely from device & watch and try again.

    I had the same problem testing this with the Today extension.

    Also check on the developer portal, that your App Id correctly has the app groups permission enabled, despite what you might see on Xcode