Streaming is available in most browsers,
and in the Developer app.
-
Support multiple users in your tvOS app
Share your living room — not your Apple TV apps. When you support profiles within your app, you can customize your experience for each person who uses Apple TV within the same house. Discover how the “Runs as Current User” feature lets someone interact with your app, download local content, and log into iCloud or Game Center, all without affecting their family or housemates. We'll show you how to implement this capability in your app, save recent data before switching profiles, handle notifications, and safeguard privacy. To get the most out of this session, you should have a basic understanding of modern tvOS frameworks and controls. Watching “Mastering the Living Room with tvOS” will give you a great overview.
Resources
- Human Interface Guidelines: Designing for tvOS
- Personalizing Your App for Each User on Apple TV
- Supporting Multiple Users in Your tvOS App
Related Videos
WWDC22
-
Download
Hello, and welcome to WWDC.
Hi, my name is Caleb.
I'm a software engineer on the tvOS team.
Today, I'm excited to share with you a great new way for your apps to support multiple users on Apple TV.
Apple TV is designed to be used by everyone in your home.
And we think it's really important that everyone who uses Apple TV is able to access all of their own content and data.
In tvOS 13, we made switching users easy by adding fast user switching right in Control Center.
We also added the TVUserManager API, so apps can link their own user profiles with users on the device.
So if your app has its own user profiles, I encourage you to watch "Mastering the Living Room with tvOS" from WWDC 2019, so you can learn how TVUserManager can help you take people right to their content.
But what if your app doesn't have its own user profiles? What if you want to store separate data for each user instead? Today, tvOS apps have access to a single set of resources for the entire device.
That means things like iCloud, Game Center, and Local Storage for your app's data are the same no matter who is using Apple TV.
But wouldn't it be great if your app knew who is using it so you could create a personalized user interface and access data from their other devices using iCloud.
In tvOS 14, we're making it really easy for your apps to do exactly that.
Now each person who uses your app will have access to all of their own data in iCloud, Achievements, and Friends, and Game Center, and even their own local data and preferences.
And because your apps can access data in each user's iCloud, everyone using Apple TV will now be able to start a game on their iPhone, continue playing on Apple TV, and easily sync their progress across all of their other devices.
Let's talk about how this works.
When your app declares support from multiple users in tvOS 14, you'll be given access to accounts and storage for the selected user.
When a new user is selected, you're rapidly given a chance to save any unsaved data, then it'll be terminated.
If your app was running in the foreground when a new user was selected, it will be automatically relaunched for the new user.
So far, I've only mentioned iCloud and Game Center by name, but I'm happy to share that all of the API we provide for storing data, both locally on Apple TV and in the cloud, support storing individual user data.
And the best part is you don't have to make any code changes.
You can keep using all of the API you're already using to read and write data and the system will take care of the rest.
By now, you're probably excited to learn how to enable this for your apps.
Only three things you need to do to support multiple users in your tvOS apps.
I'm going to go over them with you now starting with the User Management capability.
In tvOS 14 and Xcode 12, we have a new Runs as Current User option in the User Management capability.
This tells the system that you want your app to be able to support multiple users.
You can add User Management capability by navigating to your app's Signing & Capabilities editor in Xcode.
And when you add this to your app target in Xcode 12, the Runs as Current User option will be selected by default.
Next, I'd like to talk about the changes to the application lifecycle.
Earlier I mentioned that when a selected user changes, your app will be terminated.
If your app is currently running, you'll want to make sure you save any unsaved data before your process exits.
You can do that by implementing applicationWillTerminate in your app delegate.
And note, you're given a limited amount of time to perform tasks when this function is called.
So be sure to do as little work as possible to save any user data.
Let's take a look at an example.
Here I implemented applicationWillTerminate in my app delegate, First, I check whether my game has any changes that need to be saved.
If not, it's best to return right away so the system may continue switching users.
Next, I ask my game to save any unsaved data both locally and to CloudKit.
Once this function returns, the system is free to terminate my process and switch users.
So I'm using a semaphore to keep it running long enough to save data asyncronously.
There's one last change you may have to make if your app uses CloudKit subscription notifications.
When your app supports multiple users, it may get CloudKit notifications for other users on the device who've used your app.
To help make your app more efficient, you should check the new subscriptionOwnerUserRecordID property on seeking notification and compare it with the Record ID of the current CloudKit user.
If notification was meant for a different user, it can be safely discarded.
In this example, I implemented the application didReceiveRemoteNotification function in my app delegate.
I start by initializing a seeking notification object fromRemoteNotificationDictionary : userInfo.
Next I checked the notification .subscriptionOwnerUserRecordID and make sure it's equal to my game object's currentUserRecordID.
Once I know this notification is meant for the current user, I tell my game object to handle notification and call the completion handler. That's it.
That's all you need to do to prepare your app to support multiple users in tvOS 14.
And now I'd like to show you a demo.
Here I have a simple game that I ran from Xcode.
This game syncs all its data with iCloud using NSPersistentCloudKitContainer.
The main menu allows me to resume the game I started playing earlier and to view my fastest solve times from previous games.
Right now my high score is one minute and nine seconds.
If I select Resume Game, you can see the current state of the game board.
I can flip over tiles to try to find matching Memoji.
Now my friend Felipe wants to play and show off his high score.
He opens Control Center and switches to his user.
But the game stays the same. That's not very exciting.
Let's see what we can do about that.
Here I have the Xcode project for my game.
I'm going to show you how easy it is to add support from multiple users to your tvOS app or game.
First you'll navigate to the Signing & Capabilities editor of your app target.
Next, you'll add the User Management capability.
And last, you'll make sure the Runs as Current User option is selected.
That's it. Let's build and run and switch back to Apple TV.
If I resume my game or view my high scores you can see the same game data that I showed you earlier.
But now let's see what happens when I switch users.
Notice that the game was terminated, the system switched users, and the game was relaunched for Felipe.
Now when Felipe plays the game, he sees his own data.
He doesn't have an in-progress game yet, so he has an option to start a new game with no matches.
And notice his high score of 39 seconds is faster than mine.
Again, all this data is synced with iCloud and is kept completely separate for each user.
And I didn't have to make any code changes to support this.
We are really excited for your games and apps to take full advantage of multiple users in tvOS 14.
To wrap up the new Runs as Current User option and the User Management capability allows your apps to easily support multiple users in tvOS 14.
You can use existing API to store separate data for each person using your app with no code changes required.
This allows you to take full advantage of multiple users on Apple TV and provide everyone with a seamless experience when they use your app across all their devices.
Thanks for watching and enjoy the rest of the conference.
-
-
4:15 - Application Lifecycle
func applicationWillTerminate(_ application: UIApplication) { guard game.hasUnsavedChanges else { return } let semaphore = DispatchSemaphore(value: 0) game.save { _ in semaphore.signal() } semaphore.wait() }
-
5:17 - CloudKit Notifications
func application( _ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void ) { if let notification = CKNotification(fromRemoteNotificationDictionary: userInfo), notification.subscriptionOwnerUserRecordID == game.currentUserRecordID { game.handle(notification, completionHandler: completionHandler) } else { completionHandler(.noData) } }
-
-
Looking for something specific? Enter a topic above and jump straight to the good stuff.
An error occurred when submitting your query. Please check your Internet connection and try again.