I'm trying to implement ReplayKit
screen broadcast in a SwiftUI app.
According to Apple i'll need a Broadcast Upload Extension to handle video upload, as well as few delegate methods to wrap the behaviour of choosing the right extension and starting the broadcast.
I have already set up the extension, but I'm having trouble connecting it to my simple SwiftUI app, which just broadcast the iPad screen, nothing else.
Let's imagine I have a simple SwiftUI app:
import SwiftUI
struct BroadcastView: View {
@State private var broadcastStarted = false
var body: some View {
Text("Hello, World!")
Button("Toggle") {
startBroadcast()
}
}
func startBroadcast() {
if broadcastStarted {
// Do something to start
} else {
// Terminate the broadcast
}
broadcastStarted.toggle()
}
}
If I were to use Swift instead of SwiftUI, this would normally suffice:
import UIKit
import ReplayKit
class BroadcastViewController: UIViewController, RPBroadcastActivityViewControllerDelegate, RPBroadcastControllerDelegate {
var broadcastController : RPBroadcastController?
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func startBroadcast(_ sender: UIButton) {
if sender.isSelected {
sender.isSelected = false
guard (self.broadcastController == nil) else {
self.broadcastController?.finishBroadcast(handler: { (error) in
print(error as Any)
})
return
}
}else{
RPBroadcastActivityViewController .load { (activity, error) in
if error == nil {
activity?.delegate = self as RPBroadcastActivityViewControllerDelegate
self.present(activity!, animated: true, completion: nil)
}
}
}
}
// MARK: - RPBroadcastActivityViewControllerDelegate
func broadcastActivityViewController(_ broadcastAVC: RPBroadcastActivityViewController, didFinishWith broadcastController: RPBroadcastController?, error: Error?) {
self.broadcastController = broadcastController
self.broadcastController?.delegate = self
broadcastAVC.dismiss(animated: true) {
self.broadcastController?.startBroadcast(handler: { error in
print("started broadcast \(error)")
})
}
}
// MARK: - RPBroadcastControllerDelegate
func broadcastController(_ broadcastController: RPBroadcastController, didFinishWithError error: Error?) {
}
func broadcastController(_ broadcastController: RPBroadcastController, didUpdateServiceInfo serviceInfo: [String : NSCoding & NSObjectProtocol]) {
}
}
I guess I should be wrapping the controller in UIViewControllerRepresentable
in order to use it in my SwiftUI app, but I have no idea how. I have tried a few approches, but none of them worked. I also watched a few videos and read a few articles, but they all mostly explain how to show a picture picker or attach a delegate method to a text field.
Has any tried using ReplayKit broadcasting (not recording) together with SwiftUI to hand the broadcast over to Broadcast Upload Extension?
Any help wrapping the ViewController in UIViewControllerRepresentable
is appreciated.