xcode WatchKit SDK 不从 NSUserDefaults 检索数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27007288/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 06:12:25  来源:igfitidea点击:

WatchKit SDK not retrieving data from NSUserDefaults

iosobjective-cxcodexcode6watchkit

提问by Ravin Sardal

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.

我想为 Apple Watch 制作一个测试应用程序,您可以在其中设置一些 String 在您的手机上,然后它将显示在 Apple Watch 上。我决定使用 NSUserDefaults 类来存储这些数据。

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

在我的 iPhone 视图控制器中,我有一个方法可以将输入数据存储到本地存储中:

- (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.

除了某种原因,我试图检索的数据显示为空,并且标签没有被更新。

回答by Dave DeLong

+standardUserDefaultsreturns an NSUserDefaultsobject that only saves information for the current process.

+standardUserDefaults返回一个NSUserDefaults只保存当前进程信息的对象。

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

如果您想创建一个NSUserDefaults在 iOS 应用程序及其扩展程序之一之间共享数据的对象,那么您需要设置一个应用程序组容器并将其用作共享信息的基础。

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"];

启用应用组后,应用扩展及其包含的应用都可以使用 NSUserDefaults API 来共享对用户首选项的访问权限。要启用此共享,请使用 initWithSuiteName: 方法实例化一个新的 NSUserDefaults 对象,并传入共享组的标识符。例如,共享扩展可能会更新用户最近使用的共享帐户,使用如下代码:

// 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.

注意:使用 watch OS2,您不能再使用共享组容器。

回答by dredful

IMPORTANT

重要的

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

使用 watch OS2,您不能再使用共享组容器。您需要使用此堆栈溢出答案

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

“对于 OS2 - 您将需要使用 WatchConnectivity 框架并实现 WCSessionDelegate。”