How to implement Apple's SharePlay? WWDC'22

shareplay Jun 07, 2022
How to implement Apple's SharePlay? WWDC'22

With the launch of iOS 15 (in June 2021), Apple launched a new feature which allows you to experience content together with other people on a FaceTime call.

Integrating SharePlay to your applications is really straightforward once you understand how the objects work and when the methods should be triggered. So, without further ado, let's dive straight into the fundamentals.

Prerequisites

  • Developer account
  • Two devices to test it on
Note: If not possible, you can implement catalyst in your app and test SharePlay with your computer and your personal device.

Testing

As it is a FaceTime feature, it cannot be tested on simulator. You need at least two devices. Or as mentioned above, you can implement catalyst (or even make a mac app) and test the SharePlay.

How testing goes

You run the app on both of your devices. Then you call one device from FaceTime or maybe share a common FaceTime link (and reuse it throughout the testing).

Setting up SharePlay and adding entitlements

For integrating SharePlay, one simply needs to add "Group Activities" in Your Project -> Signing & Capabilities -> "+ Capability"

This will add an entitlements file to your app.

Coordination Manager

One is primarily going to use this for prepare your sessions to play and for coordination your AVPlayer.

Feel free to use the boiler plate code given the apple in their demo project.

Following apple's coordination manager code, we have prepareToPlay() which prepares for activation and according to the user's response, takes further steps.

The prepareForActivation within prepareToPlay will prompt the user for something similar to the following alert:

Once you select an option, FaceTime remembers your preference and plays according to that.

GroupActivity

This is the object you are going to use throughout the app to get the sessions and url.

The following is an example of GroupActivity object for video playing sharply object:


import GroupActivities
import AVKit

 
@available(iOS 15, *)
struct VideoGroupActivity: GroupActivity {
    
    let video: Video 
    
    static var activityIdentifier = "com.example.SharePlayApp.SharePlayApp"

     
    var metadata: GroupActivityMetadata {
        var metadata = GroupActivityMetadata()
        metadata.type = .watchTogether
        metadata.fallbackURL = Bundle.main.url(forResource: video.url, withExtension: "mp4")
        metadata.title = video.videoTitle
        return metadata
    }
}

Metadata type: There are three types available. watchTogether (for video activities), listenTogether (for audio activities) and generic (for custom SharePlay experiences)

Metadata title: This is the title that is going to appear on the FaceTime top bar once the SharePlay starts.

Fallback URL: This is the url we are going to retrieve and play when a sharplay session is detected. This may not make complete sense right now but later when we make jump to a tutorial.

How SharePlay integration works:

Once you add entitlements, add Coordinaton Manager and setup the GroupActivity, all it takes it to follow call some methods and the SharePlay will be working well!

The following is how it works:

  1. You fetch the session from GroupActivity.sessions() using task.
  2. You prepareToPlay using CoordinationManager.prepareToPlay()
  3. You join the session using session.join()
  4. You coordinate the player using player.playbackCoordinator.coordinateWithSession(session)
  5. You can leave the session (ending session locally) using sessions.leave() or end the session (end it completely for everyone) using session.end().

Yep, as easy as that!

Concluding the article, today you learned about different components of SharePlay, integrating it in your Xcode project, and learned end-to-end SharePlay implementation.

Where to go from here?

With WWDC 2022, Apple launched lot of new features for SharePlay Collaboration. Feel free to go through WWDC Sessions. The following can be your start point:

Stay tuned to know more about SharePlay, test its demo app and make yours.

Signup now to get notified about our
FREE iOS Workshops!