【问题标题】:WatchKit NSUserDefaults are nullWatchKit NSUserDefaults 为空
【发布时间】:2015-05-13 21:02:24
【问题描述】:

在使用 NSUserDefaults 设置/接收数据时进行真正的基本测试。我的 iPhone 应用程序工作正常,数据设置正常,但在手表上,它显示为空。

我为两者启用了应用程序组,并使用同一个组验证了它们是相同的。每一个的授权文件都是一样的。

在 ViewController.m 中

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

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

在 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"]);
}

我得到的只是:

玩家姓名:(空)

我必须遗漏一些如此明显的东西。当我在 iphone 端 NSLog 时,结果很好,所以我知道它保存正确。

【问题讨论】:

    标签: ios objective-c nsuserdefaults watchkit xcode-6.2


    【解决方案1】:

    您需要确保以下设置处于正确位置。

    • 检查您的应用程序和手表套件的 Entitlement.plist 文件 扩展名,它应该具有相同的应用程序组标识符。
    • 确保您在信息部分选择了正确的开发团队 每个目标。
    • 您安装了正确的移动配置文件

    您可以在 app 和 watchkit 扩展目标的功能部分测试这些设置。

    【讨论】:

    • 检查。查看。查看。这就是奇怪的部分。感到很沮丧,所以我去了开发门户,清除了我的应用程序组,以及我所有的旧测试项目,然后从头开始。第二次像魅力一样工作......哦,好吧,也许我会把它归功于Gremlins。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-16
    • 1970-01-01
    • 2013-01-13
    • 2015-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多