WatchKit NSUserDefaults are null

487 Views Asked by At

Doing a real basic test in setting / receiving data using NSUserDefaults. My iPhone app is working and setting the data just fine, but on the watch, it's coming up null.

I have the App Group enabled for both and have verified that they are identical using the same group. The entitlement files for each are bit for bit the same.

In ViewController.m

    defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.defaultmethod.share"];

[defaults setObject:playerName forKey:@"playerName"];
[defaults synchronize];

In InterfaceController.m

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    // Configure interface objects here.
    defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.defaultmethod.share"];
    [defaults synchronize];
}

- (void)willActivate {
    // This method is called when watch view controller is about to be visible to user
    [super willActivate];

    NSLog(@"playerName: %@", [defaults objectForKey:@"playerName"]);
}

And all I get back is:

playerName: (null)

I've got to be missing something so obvious. When I NSLog it on the iphone side, it comes out just fine, so I know it's saving correctly.

1

There are 1 best solutions below

1
deoKasuhal On

You need to make sure the following setting are in proper place.

  • Check Entitlement.plist file for your application and watch kit extension, it should be have same app group identifier.
  • Ensure that you selected correct development team under info section of each target.
  • You have correct mobile provisioning profiles installed

You can test these setting under capabilities section of app and watchkit extension target.