大多数浏览器和
Developer App 均支持流媒体播放。
-
在你的 tvOS app 中支持多用户
分享您的客厅?不是您的 Apple TV app。当您在 app 中支持个人资料时,您可以为在同一房屋内使用 Apple TV 的每个人自定义您的体验。探索如何“以当前用户身份运行”这个功能可让某人与您的 app 进行交互,下载本地内容并登录 iCloud 或 Game Center,所有这些都不影响其家人或室友。 我们将向您展示如何在您的 app 中实现此功能,如何在切换配置文件之前保存最新数据,处理通知并保护隐私。 要充分利用本次分享,您应该对最新 Apple tvOS 框架和控件有基本的了解。观看“用 Apple tvOS 掌握客厅”,视频将为您提供一个很好的概述。
资源
- Human Interface Guidelines: Designing for tvOS
- Personalizing Your App for Each User on Apple TV
- Supporting Multiple Users in Your tvOS App
相关视频
WWDC22
-
下载
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) } }
-
-
正在查找特定内容?在上方输入一个主题,就能直接跳转到相应的精彩内容。
提交你查询的内容时出现错误。请检查互联网连接,然后再试一次。