简体   繁体   English

无法从iPhone应用读取WatchKit设置包

[英]Can't read WatchKit settings bundle from iPhone app

In watchOS 2, it seems like you can't access the data from the WatchKit settings bundle from the watch extension itself, because it now runs on the watch instead of the host iPhone. 在watchOS 2中,您似乎无法从watch扩展本身访问WatchKit设置包中的数据,因为它现在在手表上运行,而不是在主机iPhone上运行。 A solution, which was proposed here , was to read the data on the iPhone and then transfer it to the watch. 这里提出的一种解决方案是在iPhone上读取数据,然后将其传输到手表。

My problem is, that I cannot only read the data from the watch, but even from my phone. 我的问题是,我不仅不能从手表上读取数据,甚至不能从手机上读取数据。 In ViewController.m I have the following code to get the value of a switch: 在ViewController.m中,我具有以下代码来获取开关的值:

NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"];
NSLog(@"%d", [[[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"] boolForKey:@"key_here"]);

It always returns "0", even when the switch in the settings bundle is enabled. 即使启用了设置捆绑包中的开关,它也始终返回“ 0”。

I'm using app groups and I made sure that I use the same identifier in the settings bundle and in the iPhone app: 我正在使用应用程序组,并确保在设置包和iPhone应用程序中使用相同的标识符: 观看设置捆绑包.plist

Why can't I access the bundle data from my phone? 为什么我不能从手机访问捆绑数据?

Try this: 尝试这个:

 NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"];
[userDefaults setValue:@55 forKey:@"myNumber"];   
[userDefaults synchronize];

Execute one time in iOS simulator and then remove it and add: 在iOS模拟器中执行一次,然后将其删除并添加:

    NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"];
NSLog(@"My number: %@",[userDefaults valueForKey:@"myNumber"]

And execute one more time. 并再执行一次。 You should see the value saved before 您应该看到之前保存的值

NSError *error = nil;
NSLog(@"### Directory path: %@",[[NSFileManager defaultManager]contentsOfDirectoryAtPath:[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.de.iossoftware.SocialAppDataSharing"].path error:&error]);
NSLog(@"Error: %@",error);

And in the last line you will log the file path where the group files are saved. 在最后一行中,您将记录保存组文件的文件路径。 Open it in the finder and you should see under ...../Library/Preferences/ a plist containing the NSUserdefaults created 在查找器中打开它,您应该在..... / Library / Preferences /下找到一个包含创建的NSUserdefaults的plist

I'm not sure exactly what you are trying to do as your questions isn't clear to me. 我不确定您要做什么,因为我不清楚您的问题。 However, with WatchOS 2, you cannot use shared group containers, you must use Watch Connectivity framework to sync data between the watch and phone. 但是,对于WatchOS 2,您不能使用共享组容器,必须使用Watch Connectivity框架在手表和手机之间同步数据。 This is similar to the question here: NSUserDefaults not working on Xcode beta with Watch OS2 这类似于这里的问题: NSUserDefaults在Watch OS2的Xcode beta上不起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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