我可以在 tvOS 上使用 NSUserDefaults 吗?

Can I use NSUserDefaults with tvOS?

App Programming Guide for tvOS简要地指出

There is no persistent local storage for apps on Apple TV. This means that every app developed for the new Apple TV must be able to store data in iCloud and retrieve it in a way that provides a great customer experience.

这是否意味着 NSUserDefaults 不可用?哪些机制可用于数据存储?

A​​pple 真的希望您(和 AppleTV 客户)注册并使用 iCloud。

Here is the current documentation on what to do for persistent storage with AppleTV.

介绍段:

Storage on Apple TV is limited, and there is no guarantee that information stored on the device will be available the next time a user opens your app. Also, in order to share the user’s data across multiple devices, you need to store the user’s information somewhere other than the Apple TV. Apple provides two shared storage options for Apple TV: iCloud Key-Value Storage (KVS), and CloudKit.

对于小于 1 兆的文件,您将使用 iCloud 键值存储 (KVS)。对于超过 1 兆的存储,您将使用 CloudKit。

您可以根据文档将 NSUserDefaults 用于 TVOS。

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/UserDefaults/AccessingPreferenceValues/AccessingPreferenceValues.html

根据 devforums 上一位 Apple 员工的说法,您可以在 tvOS 上使用 NSUserDefaults 来获取高达 500 kb 的数据:

https://forums.developer.apple.com/message/50696#50696

为了跟进@vegashacker 的回答,同一位 Apple 工作人员还说:

You can also store content locally using the application temp and cache directories, but note that this data will be purged.

当您的应用不再 运行 时,可能会发生清除,例如当资源紧张或重启时。

NSUserDefaults 可用,但在 tvOS 上大小有限。根据 Apple 工作人员在论坛 "NSUserDefaults is allowed, and supports up to 500KB of data." 和 "The behaviour is the same as on iOS: NSUserDefaults is persistent as long as the user does not delete the app from the device."

上的说法

同样根据NSUserDefaults.hheader "NSUserDefaultsSizeLimitExceededNotification is posted on the main queue when more data is stored in user defaults than is allowed. Currently there is no limit for local user defaults except on tvOS, where a warning notification will be posted at 512kB, and the process terminated at 1MB. For ubiquitous defaults, the limit depends on the logged in iCloud user."