本文介绍了在watchOS 2中阅读NSUserDefaults(我知道App Groups不起作用)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Apple文档说此外,iOS会自动将iOS应用程序首选项的只读副本转发给Apple Watch。您的WatchKit扩展程序可以使用NSUserDefaults对象读取这些首选项......

Apple documentation says "Also, iOS automatically forwards a read-only copy of your iOS app’s preferences to Apple Watch. Your WatchKit extension can read those preferences using an NSUserDefaults object...". https://developer.apple.com/library/prerelease/watchos/documentation/General/Conceptual/WatchKitProgrammingGuide/SharingData.html#//apple_ref/doc/uid/TP40014969-CH29-SW1

但是我的WatchKit应用程序中的读取默认值已保存在我的iPhone应用程序中,我做错了吗?

But I can't read defaults in my WatchKit app that were saved in my iPhone app, am I doing something wrong?

在WatchKit应用程序中,我是否可以保存到(仅限WatchKit)默认值,或者甚至不能正常工作?

In the WatchKit app, can I even save to (a WatchKit only) defaults, or does that not even work?

iPhone 数据

let defaults = NSUserDefaults.standardUserDefaults()
defaults.setObject(dataArray, forKey: "DataSaved"

观看 ExtensionDelegate

let defaults = NSUserDefaults.standardUserDefaults()
print(defaults.arrayForKey("DataSaved"))
print(defaults.objectForKey("DataSaved"))

arrayForKey objectForKey 用于打印 nil

我一定做错了什么,你知道它会是什么吗?

I must be doing something wrong, do you know what it would be?

推荐答案

<$ c $之间的区别c> NSUserDefaults

Difference between NSUserDefaults

NSUserDefaults 是任何内存的单位设备,所以它在iOS和watchOS以及tvOS等方面完全不同。

NSUserDefaults is a unit of memory on any device, so it is completely different in iOS and watchOS, as well in tvOS, etc.

例如,如果你运行一个添加名为key1的新密码的代码当您请求读取其数据时,iOS上的 NSUserDefaults (假设您使用 NSUserDefaults.standardUserDefaults())在watchOS上,它会为你提供一个空字典。

For example, if you run a code that adds a new key called "key1" to NSUserDefaults on iOS (assuming you are using NSUserDefaults.standardUserDefaults()) , when you request to read its data on watchOS, it gives you an empty dictionary.

这是因为它是第一次将字典存储在iOS App Target中而不是在两个设备之间的共享空间中。当WatchKit扩展想要使用 NSUserDefaults 时,它会读取保存在WatchKit Target中的(空)字典,而不是iOS Target中的(已填充)字典。

That's because it the first time, you store the dictionary only in iOS App Target and not in a shared space between two devices. When the WatchKit extension wants to use NSUserDefaults, it reads the (empty) dictionary saved in WatchKit Target and not the (filled) dictionary in iOS Target.

分享数据

在watchOS和iOS之间有一些共享词典的方法:

There are some ways of sharing dictionaries between watchOS and iOS:

1-在watchOS 2中使用 WatchConnectivity 框架,您可以在两个目标之间传递数据。您可以在 WCSession 类中使用 sendMessage 或类似函数来执行此操作。只是不要忘记将 WCSessionDelegate 添加到 InterfaceController (在watchOS中)或 ViewController (在iOS中)应用程序的类和应用程序代表能够从 WatchConnectivity 接收数据。

1- Using WatchConnectivity framework in watchOS 2, you could pass data between two targets. You can use sendMessage or similar functions in WCSession classes to do so. Just don't forget to add WCSessionDelegate to your InterfaceController (in watchOS) or ViewController (in iOS) classes and app delegates for the app to be able to receive data from WatchConnectivity.

2-使用在线/云端存储,您可以从iOS App上传字典,然后将其下载到watchOS App。但是,这需要Internet连接,并且在许多情况下不适用,例如,如果您的应用程序不需要连接到Internet来执行其他任务,并且您强制用户仅为了同步而连接到Internet两个目标之间的简单字典。

2- Using online/cloud storages, you could upload the dictionary from iOS App there, and then download it to watchOS App. However, this needs an Internet connection, and is not suitable in many cases, for example if your app does not need to connect to the Internet to do other tasks, and you force the user to connect to the Internet just in order to sync a simple dictionary between the two targets.

3-使用应用程序组(如果你使用的是watchOS 1),我没有详细描述,因为它已经过时了你可能不想只为watchOS 1制作你的应用程序。

3- Using App Groups (if you are using watchOS 1), which I don't describe it more, because it is outdated and you probably don't want to make your app just for watchOS 1.

结论

1- NSUserDefaults 分别在watchOS和iOS中返回两个不同的词典。

1- NSUserDefaults returns two different dictionaries in watchOS and iOS, respectively.

2-要在watchOS和iOS之间共享数据,你可以使用 WatchConnectivity ,互联网(不推荐)和应用程序组(在watchOS 1中)。

2- To share data between watchOS and iOS, you could use WatchConnectivity, the Internet (not recommended) and App Groups (in watchOS 1).

更多资源

有关 WatchConnectivity 的更多详细信息,您问题的现代答案,您应该查看。

For more details about WatchConnectivity, the modern answer to your question, you should check out Apple Documentation in Apple Developer Portal.

另外你应该看到讨论了watchOS 2中包含的这个伟大的框架。

Also you should see this WWDC session talking about this great framework included in watchOS 2.

这篇关于在watchOS 2中阅读NSUserDefaults(我知道App Groups不起作用)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 20:18