【问题标题】:NSUserDefaults from AppGroup is empty in Watch App来自 AppGroup 的 NSUserDefaults 在 Watch App 中为空
【发布时间】:2015-10-14 19:02:25
【问题描述】:

我的应用有以下目标:

  • iOS 应用
  • 今日扩展
  • Apple Watch 应用
  • Apple Watch 扩展程序

所有这些目标都具有“应用组”功能,并选择了正确的组。在主 iOS 应用程序中,我像这样同步设置:

NSUserDefaults * sharedDefaults = [[NSUserDefaults alloc]initWithSuiteName:@"group.foo.bar"];
if([sharedDefaults respondsToSelector:@selector(setObject:forKey:)]){
    // Example Value
    [sharedDefaults setObject:@"TestString" forKey:@"TestString"];
    [sharedDefaults synchronize];
}

Today Extension 使用以下方法获取设置,效果很好:

NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.foo.bar"];
NSLog(@"String: %@", [defaults stringForKey:@"TestString"]);
// String: TestString

手表扩展程序使用与今天扩展程序相同的代码行来获取设置,但是它不起作用。在控制台中我看到String: (null)。我做错了什么?

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    您将手表目标创建为 watchOS WatchKit 应用程序 (watchOS 2) 还是 iOS 应用程序扩展 (watchOS 1)?如果针对 watchOS 2,根据transition guideYou cannot rely on a shared group container to exchange files with your iOS app. Fetching files involves transferring them wirelessly to Apple Watch.

    【讨论】:

      【解决方案2】:

      请参考settings bundle not working on watchOS 2 显然,文档并不清楚使用首选项可以做什么和不能做什么,我发现的唯一方法是确保我复制所需的用户默认值并手动保持同步(@987654322 @)。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-31
        • 1970-01-01
        相关资源
        最近更新 更多