简体   繁体   中英

Get Data from Parent to WatchKit with NSUserDefaults

since days I try to get some data vom the parent to my WatchKitApp. I added a group to the parent, to WatchApp and WatchApp Extension. Then I use this code to save the data in Objective-C:

NSUserDefaults *defaults = [[NSUserDefaults alloc]  initWithSuiteName:@"group.mygroup"];
[defaults setInteger:5 forKey:@"test"];        
[defaults synchronize];

Then I try to get the data from the WatchKit in Swift:

let defaults = NSUserDefaults(suiteName: "group.mygroup")
let integer = defaults!.integerForKey("test")
print("Test \(integer)")

In my opinion the code and setting are right. I don't know what I'am doing wrong.

Make sure your suite name fits the group name you have already set up for your group. To look up your group name, go to the project editor, select your app as the target and go to capabilities. Expand the app group cell and look up the name there.

1:

在此处输入图片说明

2:

在此处输入图片说明

If you are using watchOS 2.0, shared groups are no longer supported, you must use Watch Connectivity framework instead. See my answer to a similar question here: stackoverflow.com/questions/30851729/… I would recommend switching now as the OS will be out in the fall. – rmp 15 hours ago

Yes this was my Problem thx :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM