WatchKit Extension看不到带有App Group的NSUserDefaults中保存的数据 [英] WatchKit Extension is not seeing data saved in NSUserDefaults with App Group

查看:201
本文介绍了WatchKit Extension看不到带有App Group的NSUserDefaults中保存的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经处理了很多SO帖子,并且这个USED可以正常工作,但是它停止了工作.我不确定发生了什么.我使用watchOS 1.0开发了这个iPhone + WatchKit应用,一切正常.

I've been through a ton of SO posts, and this USED to work, but it stopped working. I'm not sure what happened. I developed this iPhone+WatchKit app with watchOS 1.0 and everything worked fine.

我已经将我的应用程序,项目和Apple Watch升级到watchOS 2.0,现在我无法使用我的应用程序组通过NSUserDefaults获取任何数据.

I've upgraded my app, project, and Apple Watch to watchOS 2.0, and now I can't get any data via NSUserDefaults using my App Group.

  1. App Groups已在主机App和WatchKit Extension上的Xcode中启用.我什至还尝试为WatchKit App打开它.

  1. App Groups is enabled in Xcode on the host App, and WatchKit Extension. I even tried turning it on for the WatchKit App as well.

我的群组名称称为"group.com.mycompany.myapp"(带有我的真实公司名称和应用名称),并且在所有目标上都已选择该群组名称.

My group name is called "group.com.mycompany.myapp" (with my real company name and app name) and it's selected on all of the targets.

我已经确认主机应用程序的构建设置和WatchKit扩展引用了权利文件,并检查了这些权利文件是否包含所选应用程序组的应用程序组安全性选项.

I've confirmed the Build settings for my host app and WatchKit extension reference the entitlements files and I've checked that those entitlements files contain the app groups security option for my chosen app group.

我已确保主机应用程序,watchkit扩展程序和watchkit应用程序的捆绑包标识符不同.我分别使用"com.mycompany.myapp","com.mycompany.myapp.watchkitextension"和"com.mycompany.myapp.watchkitapp".

I've made sure the bundle identifiers are different for the host app, watchkit extension, and watchkit app. I use "com.mycompany.myapp", "com.mycompany.myapp.watchkitextension", and "com.mycompany.myapp.watchkitapp", respectively.

这是我的Xcode配置的一些截图,用于主机应用程序,watchkit扩展程序和watchkit应用程序,以防万一:

Here are some screenshots of my Xcode config for the host app, watchkit extension, and watchkit app, just in case:

这是我的主机应用程序中的代码,能够正确读取数据:

Here is the code in my host app that is able to read the data properly:

NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kAppGroupName];
NSLog(@"app defaults: %@", [defaults dictionaryRepresentation]);

这会在我的控制台中生成此内容,其中kKeyEmployeeIdkKeyStoreNumber是我要在主机和手表之间共享的数据.

That produces this in my console, with kKeyEmployeeId and kKeyStoreNumber being the data that I'm trying to share between the host and watch.

2015-12-13 13:51:35.618 MyApp[27516:16126253] app defaults: {
    AppleLanguages =     (
        "en-US",
        en
    );
    INNextHearbeatDate = "472211882.83309";
    NSInterfaceStyle = macintosh;
    NSLanguages =     (
        "en-US",
        en
    );
    "com.apple.content-rating.AppRating" = 1000;
    "com.apple.content-rating.ExplicitBooksAllowed" = 1;
    "com.apple.content-rating.ExplicitMusicPodcastsAllowed" = 1;
    "com.apple.content-rating.MovieRating" = 1000;
    "com.apple.content-rating.TVShowRating" = 1000;
    kKeyEmployeeId = Kenny;
    kKeyStoreNumber = 001;
}

在我的WatchKit扩展程序中,我具有相同的代码,但是没有提供我需要的两个key:value对:

In my WatchKit Extension, I have the same code, but it doesn't provide me the two key:value pairs that I need:

- (void)awakeWithContext:(id)context {
    [super awakeWithContext:context];

    NSUserDefaults *defaults = [[NSUserDefaults alloc] initWithSuiteName:kAppGroupName];
    NSLog(@"defaults: %@", [defaults dictionaryRepresentation]);

}

它产生的结果与上面的非常相似,但是没有我真正需要的两个键:

It produces this, which is very similar to the one above, but without the two keys I really need:

2015-12-13 13:28:29.840 MyApp WatchKit Extension[720:322804] defaults: {
    AppleLanguages =     (
        en
    );
    INNextHearbeatDate = "472434306.599217";
    NSInterfaceStyle = macintosh;
    NSLanguages =     (
        en
    );
    "com.apple.content-rating.AppRating" = 1000;
    "com.apple.content-rating.ExplicitBooksAllowed" = 1;
    "com.apple.content-rating.ExplicitMusicPodcastsAllowed" = 1;
    "com.apple.content-rating.MovieRating" = 1000;
    "com.apple.content-rating.TVShowRating" = 1000;
}

kAppGroupName和其他常量的定义如下:

The kAppGroupName and other constants are defined like so:

NSString * const kAppGroupName = @"group.com.inadaydevelopment.MyApp";
NSString * const kKeyStoreNumber = @"kKeyStoreNumber";
NSString * const kKeyEmployeeId = @"kKeyEmployeeId";

推荐答案

在Watch OS1中,此方法有效,但在Watch OS2中进行了一些更改.您需要使用

In Watch OS1 this worked, but in Watch OS2 there has been some changes. You need to use something called WatchConnectivity to send the data you want to save to the watch. Then when the watch receives the data you sent to it, save it to the Apple watch's default NSUserDefaults.

WCSession.defaultSession()将返回WCSession单例,用于在iOS和Watch应用之间传输数据.

WCSession.defaultSession() will return the WCSession singleton for transferring data between your iOS and Watch app.

此处是一个教程和示例.

这篇关于WatchKit Extension看不到带有App Group的NSUserDefaults中保存的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆