无法从iPhone应用程序读取WatchKit设置捆绑 [英] Can't read WatchKit settings bundle from iPhone app

查看:105
本文介绍了无法从iPhone应用程序读取WatchKit设置捆绑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在watchOS 2中,您似乎无法从watch扩展本身访问WatchKit设置包中的数据,因为它现在在手表上运行,而不是在主机iPhone上运行.在此处提出的解决方案,是要在iPhone上读取数据,然后将其传输到手表.

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. A solution, which was proposed here, was to read the data on the iPhone and then transfer it to the watch.

我的问题是,我不仅要从手表上读取数据,甚至要从手机上读取数据.在ViewController.m中,我具有以下代码来获取开关的值:

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:

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

即使启用了设置捆绑包中的开关,它也始终返回"0".

It always returns "0", even when the switch in the settings bundle is enabled.

我正在使用应用程序组,并且确保在设置包和iPhone应用程序中使用相同的标识符:

I'm using app groups and I made sure that I use the same identifier in the settings bundle and in the iPhone app:

为什么我不能从手机访问捆绑数据?

Why can't I access the bundle data from my phone?

推荐答案

尝试一下:

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

在iOS模拟器中执行一次,然后将其删除并添加:

Execute one time in iOS simulator and then remove it and add:

    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);

在最后一行中,您将记录保存组文件的文件路径.在查找器中打开它,您应该在...../Library/Preferences/下创建一个包含创建的NSUserdefaults的plist.

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

这篇关于无法从iPhone应用程序读取WatchKit设置捆绑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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