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

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

问题描述

在 watchOS 2 中,您似乎无法从手表扩展程序本身访问 WatchKit 设置包中的数据,因为它现在在手表而不是主机 iPhone 上运行.在

为什么我无法从手机访问捆绑数据?

解决方案

试试这个:

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

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

 NSUserDefaults *userDefaults = [[NSUserDefaults alloc] initWithSuiteName:@"group.de.iossoftware.SocialAppDataSharing"];NSLog(@"我的号码:%@",[userDefaults valueForKey:@"myNumber"]

再执行一次.你应该看到之前保存的值

NSError *error = nil;NSLog(@"### 目录路径:%@",[[NSFileManager defaultManager]contentsOfDirectoryAtPath:[[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:@"group.de.iossoftware.SocialAppDataSharing"].path 错误:&error]);NSLog(@"错误:%@",错误);

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

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.

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

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

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?

解决方案

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:

    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

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

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