25

I wanted to make a test app for the Apple watch in which you can set some String on your phone, and then it will be displayed on the Apple Watch. I decided to use NSUserDefaults class to store this data.

In my view controller for the iPhone I have a method which takes the input and stores into local storage:

- (IBAction)saveInfo:(id)sender {

    NSString *userInput = [myTextField text];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults setObject:userInput forKey:@"savedUserInput"];

    [defaults synchronize];

    self.myLabel.text = [defaults stringForKey:@"savedUserInput"];
}

And in my watch interface controller I have a method that retrieves the data and displays it:

- (IBAction)showText2 {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    [defaults synchronize];

    self.myLabel2.text = [defaults stringForKey:@"savedUserInput"];
}

Except for some reason the data I am trying to retrieve is shown as null, and the label is not being updated.

Ravin Sardal
  • 717
  • 1
  • 13
  • 23

2 Answers2

31

+standardUserDefaults returns an NSUserDefaults object that only saves information for the current process.

If you want to create an NSUserDefaults object that shares data between your iOS app and one of its extensions, then you'll need to set up an app group container and use that as the basis for sharing information.

From the linked documentation:

After you enable app groups, an app extension and its containing app can both use the NSUserDefaults API to share access to user preferences. To enable this sharing, use the initWithSuiteName: method to instantiate a new NSUserDefaults object, passing in the identifier of the shared group. For example, a Share extension might update the user’s most recently used sharing account, using code like this:

// Create and share access to an NSUserDefaults object.
NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.com.example.domain.MyShareExtension"];

// Use the shared user defaults object to update the user's account.
[mySharedDefaults setObject:theAccountName forKey:@"lastAccountName"];

NOTE: With watch OS2 you can no longer use shared group containers.

dredful
  • 4,378
  • 2
  • 35
  • 54
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498
  • 1
    I set up the group container in DevPortal and have put the appropriate group name, but the data is still not being shared between the phone and the watch. Does it take a while to update within apple? – Ravin Sardal Nov 19 '14 at 02:54
  • 1
    @RavinSardal I believe the suite name you'll want to use is `@"group.{whatever your group name is}"` – Dave DeLong Nov 19 '14 at 03:49
  • 1
    Dave, a WatchKit target does not have a "capabilities" tab in Xcode. Do WatchKit apps automatically inherit app group settings? I've used app groups for extensions but I don't see where to add one for WatchKit. – Tom Harrington Nov 19 '14 at 04:13
  • 1
    @TomHarrington A Watch App does not have Capabilities, but the WatchKit Extension does; since it's the extension that contains the executable code, it's the extension that needs the app group capability. – Dave DeLong Nov 19 '14 at 04:33
  • @DaveDeLong got it, I was looking at the wrong target. Thanks. – Tom Harrington Nov 19 '14 at 04:40
  • 2
    @DaveDeLong Exactly. Thanks! Though in docs apple says: NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"com.example.domain.MyShareExtension"]; But the groupId can start only with "group." prefix. So the correct code is: NSUserDefaults *mySharedDefaults = [[NSUserDefaults alloc] initWithSuiteName: @"group.myGroupId"]; – Naloiko Eugene Mar 19 '15 at 18:15
  • @DaveDeLong Your comment for watch kit extension and its capability saved my day. Would please stress that one need to set the capability in watch extension in your answer so that others who missed the comment will see it? – CarmeloS Apr 13 '15 at 15:10
  • 1
    Didn't seem to work for me. Using my group in both the app and the extension. App seems to work fine, but the extension/watch doesn't seem to be able to pull that data (getting null). – Kudit Apr 24 '15 at 01:34
  • +1 for the Capability on the Extension. Once I went to the Extension target and enabled that Capability, then his worked perfect. @DaveDeLong, I recommend adding that to your answer if you are allowed to edit it still. – Kenny Wyland Jul 10 '15 at 21:18
  • Is it working in WatchOS2. I am struggling to access shared NSUserDefaults but not able to access, always getting (null) – Mrug Apr 04 '16 at 07:50
  • 1
    ***IMPORTANT*** - This no longer works in WatchOS2 - http://stackoverflow.com/questions/30851729/nsuserdefaults-not-working-on-xcode-beta-with-watch-os2#comment51903891_30851729 – dredful Apr 06 '16 at 15:30
8

IMPORTANT

With watch OS2 you can no longer use shared group containers. You need to use this stack overflow answer.

"For OS2 - you will need to use the WatchConnectivity frameworks and implement the WCSessionDelegate."

Community
  • 1
  • 1
dredful
  • 4,378
  • 2
  • 35
  • 54