2

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.

kernelpanic
  • 2,876
  • 3
  • 34
  • 58
  • If you know how to do it (and actually did it) with UIKit why do you need SwiftUI? – Asperi Nov 15 '20 at 16:34
  • @Asperi because I already have a functioning app written in SwiftUI and the only functionality that is missing = live broadcast. Rewriting the app in pure swift feels like a setback and is going to be my last resort if I don't find a viable solution in SwiftUI – kernelpanic Nov 16 '20 at 07:59
  • SwiftUI offers a UiVIewControllerRepresentable; to piggyback on @Asperi, if this is done in UIKit, you could leave it there and bridge it without rewriting it. – binaryPilot84 Nov 28 '20 at 13:12

0 Answers0