1

Is sharing UserDefaults between iOS and tvOS still possible?

In my Xcode project I use UserDefaults to share data between my iOS target and my tvOS target. I always receive nil values from my UserDefaults when I try to get data back from my tvOS app.

These are the steps I took to share data:

1: Add App Groups for both targets. Both targets use the same App Group ID: group.nl.mycompany.myappname.

I use the .nl domain but this should be fine since this also worked for my other projects.

2: Confirm both targets have the same deployment target. I tried using 10.0 and 11.0.

3: Validate the myproject.entitlements that everything is set OK.

4: Validate that on developer.apple.com the App Group is enabled for my bundle identifier.

5: Both targets have the same bundle ID. I also tried using 2 different bundle identifiers.

6: The way I write to UserDefaults from my iOS app:

guard let defaults = UserDefaults(suiteName: "group.nl.mycompany.myappname") else { return }
defaults.set("Apple", forKey: "username")
defaults.synchronize()

I confirm this works in my iOS app by getting the value like so:

guard let defaults = UserDefaults(suiteName: "group.nl.mycompany.myappname") else { return nil }
defaults.synchronize()
let name = defaults.string(forKey: "username")

This indeed returns "Apple".

7: Opening my tvOS app and calling this code returns nil:

guard let defaults = UserDefaults(suiteName: "group.nl.mycompany.myappname") else { return nil }
defaults.synchronize()
let name = defaults.string(forKey: "username")

Is it possible that UserDefaults sharing has been removed? Something similar happened to sharing UserDefaults between your phone and watch link here. I also read that the maximum size of UserDefaults is 500kb for the AppleTV but saving this simple string should be fine.

jscs
  • 63,694
  • 13
  • 151
  • 195
Hapeki
  • 2,153
  • 1
  • 20
  • 36
  • Where did you read that maximum size of `UserDefaults` is `500kb`? I guess there is no limit until there is enough space in your device. – TheTiger Apr 04 '18 at 12:57
  • @TheTiger Thank you for your reply. I found that on this link: https://stackoverflow.com/a/32781011/6414904 – Hapeki Apr 04 '18 at 12:58
  • Now may be correct after specifying for `tvOS`. – TheTiger Apr 04 '18 at 13:02
  • I read the forum answer, although that question is for `tvOS` but answer doesn't specify this. Apple guy clearly said that `NSUserDefaults is allowed, and supports up to 500KB of data.` I wondered!! – TheTiger Apr 04 '18 at 13:05
  • @TheTiger It indeed is allowed but it seems there's a limit on the size of data based on the answer. – Hapeki Apr 04 '18 at 13:07
  • 1
    [About Limit Another SO Answer](https://stackoverflow.com/questions/7510123/is-there-any-limit-in-storing-values-in-nsuserdefaults) – TheTiger Apr 04 '18 at 13:08

1 Answers1

5

Apple clearly states in the UserDefaults documentation that

With the exception of managed devices in educational institutions, a user’s defaults are stored locally on a single device, and persisted for backup and restore. To synchronize preferences and other data across a user’s connected devices, use NSUbiquitousKeyValueStore instead.

As it says, you should use iCloud-based NSUbiquitousKeyValueStore for synchronized data storage.

As for its (NSUbiquitousKeyValueStore) limits, the documentation says

The total amount of space available in your app’s key-value store, for a given user, is 1 MB. There is a per-key value size limit of 1 MB, and a maximum of 1024 keys.

Dan Karbayev
  • 2,870
  • 2
  • 19
  • 28
  • My bad I totally missed this from the documentation but this indeed solves the issue. Thank you! – Hapeki Apr 04 '18 at 13:09
  • `There is a per-key value size limit of 1 MB, and a maximum of 1024 keys.` Oh that is why few are saying that there is not limit so they might be considering multiple keys. Good to know! – TheTiger Apr 04 '18 at 13:10
  • @TheTiger please note that that's for `NSUbiquitousKeyValueStore` - not `UserDefaults` (which is not limited by space in theory) – Dan Karbayev Apr 04 '18 at 13:12
  • Okay, but why [this answer](https://stackoverflow.com/questions/32644622/can-i-use-nsuserdefaults-with-tvos/32781011#32781011) is for `UserDefaults` ? – TheTiger Apr 04 '18 at 13:17