4
votes

I am trying to use the App Groups Entitlement in my app for NSUserDefaults handling data between the iPhone app and the WatchKit Extension.

I went to Capabilities in the iPhone target, and turned on App Groups, and made sure that group.com.316apps.iPrayed was selected. I then went to the WatchKit Extension Target and did the same for its Capabilities.

On the iPhone side of the app I put in the following code:

PFUser *me2 = [PFUser currentUser];
        NSLog(@"USERNAME%@", me2.username);
                NSUserDefaults *testDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.316apps.iPrayed"];
        [testDefaults setObject:me2.username forKey:@"username"];
        [testDefaults setObject:me2.password forKey:@"password"];

        [testDefaults synchronize];

In the WatchKit InterfaceController, I have the following, but the NSLog always shows 'null'

NSUserDefaults *testDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.316apps.iPrayed"];
    NSString *theirUser = [testDefaults objectForKey:@"username"];
    NSString *theirPass = [testDefaults objectForKey:@"password"];
    NSLog(@"%@", theirUser);

Why is the WatchKit not reading the NSUserDefaults properly?

NOTE My bundle identifier is different for the watch kit extension target. Do I need a new Apple ID for that?

1

1 Answers

0
votes

It is OK your bundle identifiers are not the same, however they should be in this format

The App Bundle ID -> com.companyName.productName
The Extension Bundle ID -> com.companyName.productName.extensionName

If they follow the formats above then refer to the answer below

You need to add an observer that knows when a value in NSUserDefaults has changed.

Refer to this code:

[[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(userDefaultsDidChange:)
                                                 name:NSUserDefaultsDidChangeNotification
                                               object:nil];