18

The App Programming Guide for tvOS briefly states that

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.

Does this mean NSUserDefaults is unavailable? What mechanisms are available for data storage?

jtbandes
  • 115,675
  • 35
  • 233
  • 266

5 Answers5

26

According to an Apple Staff member on the devforums, you can use NSUserDefaults on tvOS for up to 500 kb of data:

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

vegashacker
  • 835
  • 9
  • 14
13

You can use NSUserDefaults for TVOS as per documentation.

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Usama
  • 548
  • 3
  • 12
  • well drat! +1 to you for [noticing most of Foundation is also built into tvOS](https://developer.apple.com/library/prerelease/tvos/navigation/#section=Frameworks&topic=Foundation). – Michael Dautermann Sep 18 '15 at 05:51
  • 2
    This is a document that has existed for >10 years. Yet it doesn't say anything specific about tvOS. I'm wondering if anyone has tested this or found more official/specific documentation stating that it can or can't be used... – jtbandes Sep 18 '15 at 06:10
  • This is the correct answer. On tvOS, NSUserDefaults is available, but it will only persist a small amount of data. – lemnar Sep 19 '15 at 18:51
  • NSUserDefaults are always considered to store small amount of data – Usama Sep 21 '15 at 05:15
5

Apple really wants you (and AppleTV customers) to sign up for and use iCloud.

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

The introductory paragraph:

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.

For files less than 1 meg, you'll use iCloud Key-Value Storage (KVS). For storage more than 1 Meg, you'll use CloudKit.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
2

To follow up @vegashacker's answer, the same Apple staff member also said:

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

The purge is likely to happen when you app is no longer running, e.g. when resources are under pressure, or at reboot.

Echelon
  • 7,306
  • 1
  • 36
  • 34
0

NSUserDefaults is available but has a limited size on tvOS. According to Apple staff on forum "NSUserDefaults is allowed, and supports up to 500KB of data." and also "The behaviour is the same as on iOS: NSUserDefaults is persistent as long as the user does not delete the app from the device."

Also according to NSUserDefaults.h header "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."

Leszek Szary
  • 9,763
  • 4
  • 55
  • 62