Swift Tutorial: SharePlay and GroupActivities API Example

With iOS 15.1 you can integrate your app into FaceTime using the new SharePlay technology.

Below is the simplest implementation of SharePlay and GroupActivities API that I could write, and it can be customized based on your application’s need. I have added comments in the code to help you understand the code better.

Few things to keep in mind:

  • Don’t forget to add GroupActivities entitlement to your project.
  • You will need a paid Apple Developer account to be able to add the entitlement and test
  • You won’t be able to test on the simulator, so you will need two devices with two different FaceTime accounts. You can sign into a different Apple ID into FaceTime only in the Settings.

Here is the code below of the whole ViewController with comments:

//
//  ViewController.swift
//  SharePlay Demo
//
//  Created by Kais K. on 02/14/2022.
//

import UIKit
import GroupActivities // Step 1

// This is your custom GroupActivity, this could be in its own file if needed or part of this ViewController
struct ShowAlertSharePlayActivity: GroupActivity {
    var metadata: GroupActivityMetadata {
        var metadata = GroupActivityMetadata()
        metadata.title = NSLocalizedString("Activity Title", comment: "") // The title is visible to the user
        metadata.type = .generic // This is important to set as generic to define a custom activity
        return metadata
    }
}

class ViewController: UIViewController {
    
    var groupSession: GroupSession<ShowAlertSharePlayActivity>? //Group Session of activty type 
    var messenger: GroupSessionMessenger? // The Messenger of the session to send/receive messages between devices
    
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "Show Alert Example"
        
        // On viewDidLoad we need to configure the session
        Task {
            for await session in ShowAlertSharePlayActivity.sessions(){
                configureGroupSession(session) // see implementation below
            }
        }
        
    }
    
    // Link this action to a button on your app to activate the SharePlay activity after the FaceTime call has started and you launched the app
    @IBAction func activateGroupActivity(_ sender: Any) {
        ShowAlertSharePlayActivity().activate()
    }
    
    // Link this action to a button to trigger an alert on their side
    @IBAction func showAlertToOtherUser(_ sender: UIBarButtonItem) {
        if let messenger = messenger {
            Task {
                do {
                    try await messenger.send("Your text here") // or this could be a custom define struct that can be sent to the other device to process. See references below for more details.
                } catch {
                    print("error")
                }
            }
        }
    }
    
    // Func to configure the group session and create a task to wait/listen for any messages that could come via SharePlay
    func configureGroupSession(_ groupSession: GroupSession<ShowAlertSharePlayActivity>) {
        self.groupSession = groupSession
        let messenger = GroupSessionMessenger(session: groupSession)
        self.messenger = messenger
        
        // wait and listen for any messages that could come via SharePlay and handle it
        Task.detached { [weak self] in
            for await (message, _) in messenger.messages(of: String.self) {
                await self?.handle(message) // custom func to handle the received message. See below.
            }
        }
        groupSession.join() // join the session if available
    }
    
    // Func to handle the received message, the param is String, but this also could be a custom struct. See references below.
    func handle(_ message: String) {
        showAlertFromFaceTime()
    }
    
    // Func to be triggered when message received via SharePlay
    func showAlertFromFaceTime() {
        let alert = UIAlertController(title: "Hey", message: "This alert was sent via SharePlay!", preferredStyle: .alert)
        let cancelAction = UIAlertAction(title: "Dismiss", style: .cancel)
        alert.addAction(cancelAction)
        present(alert, animated: true)
    }
    
}

Useful References:

Leave a Comment