Implementing multi-user support in tvOS 14
Turns out it is pretty easy change to make with potential benefits for your users.Published: Oct. 20, 2020 Sponsored App Store
Starting with tvOS 14 we can support multiple users in Apple TV apps. I have recently implemented this in my app Chill Zones and it was pretty quick process. Since Chill Zones loads data from personal iCloud account, having multi-user support makes the experience much better as everyone signed into Apple TV can get their videos instead of the app just using the default user account.
The basic multi-user support does not involve any new code at all. You just need to add new capability available in Xcode 12+ called "User management".
This automatically checks "Runs as Current User" which means the app will automatically access data associated with the user currently selected as active on Apple TV. If you decide to switch the user while the app is running, the system will automatically terminate it, user sees nice loading indicator informing them about switch in progress and the app resumes under newly selected account.
Handling user data
This is great because you don't have to worry about any leftover state. Of course if you need to save any user data, you need to implement
Immediately after turning this on, I could switch users in Chill Zones and the app automatically loaded videos for the current user.
It is not just iCloud, each user gets their own app container, which means stuff like
UserDefaults, file storage and other preferences work on per-user basis. I ended up using
NSUbiquitousKeyValueStore which works a lot like
UserDefaults but is automatically shared across user's devices via iCloud.
Just don't forget the soft 512 kB limit for
UserDefaults on tvOS.
Handling remote notifications
In Chill Zones I am using CloudKit subscription and its notifications will be delivered without accounting for the current user. This means you need to filter these notifications manually. There is property
subscriptionOwnerUserRecordID which tells you the ID of the user to whom the notification is intended to.
You can fetch current user ID with the method
fetchUserRecordID available on the
CKContainer. In my case I am storing this and then comparing if notification arrives.
And I believe that is all there is to supporting multiple users on Apple TV with tvOS 14. Who knows maybe Apple is tentatively moving towards supporting multiple users on iPads.
Thanks for reading.
Uses: Xcode 12 & Swift 5.3