iosswiftreplaykit

What are the delegate methods for ReplayKit's initial alert choices?


When the the user first decides to use ReplayKit there is an alert that appears. It gives 3 choices:

-Record Screen and Microphone
-Record Screen Only
-Don’t Allow

What are the delegate methods so that I can find out which option a user choose?

enter image description here


Solution

  • There aren't any delegate methods to determine which option a user chooses. You have to use the completionHandler and .isMicrophoneEnabled to determine the chosen option.

    Once an option is chosen the completionHandler is called:

    1. If the user chooses Don't Allow then the error code will run

    2. If the user chooses Record Screen & Microphone then .isMicrophoneEnabled will get set to true

    3. If the user chooses Record Screen Only then .isMicrophoneEnabled will get set to false

    Inside the completionHandler you can check to see their choice then do whatever you need to do from that point on. Read the 2 comments in the completionHandler part of the code below.

    let recorder = RPScreenRecorder.shared()
    
    recorder.startCapture(handler: { [weak self](buffer, bufferType, err) in
    
        // ...
    
    }, completionHandler: { (error) in
    
        // 1. If the user chooses "Dont'Allow", the error message will print "The user declined application recording". Outside of that if an actual error occurs something else will print
        if let error = error {
            print(error.localizedDescription)
            print("The user choose Don't Allow")
            return
        }
    
        // 2. Check the other 2 options
        if self.recorder.isMicrophoneEnabled {
    
            print("The user choose Record Screen & Microphone")
    
        } else {
    
            print("The user choose Record Screen Only")
        }
    })
    

    The safe way to do this so that you know how to respond to each error code is to use a switch statement on the error codes:

    }, completionHandler: { (error) in
    
        if let error = error as NSError? {
    
            let rpRecordingErrorCode = RPRecordingErrorCode(rawValue: error.code)
            self.errorCodeResponse(rpRecordingErrorCode)
        }
    })
    
    func errorCodeResponse(_ error: RPRecordingErrorCode?) {
    
        guard let error = error else { return }
    
        switch error {
    
        case .unknown:
            print("Error cause unknown")
        case .userDeclined:
            print("User declined recording request.")
        case .disabled:
            print("Recording disabled via parental controls.")
        case .failedToStart:
            print("Recording failed to start.")
        case .failed:
            print("Recording error occurred.")
        case .insufficientStorage:
            print("Not enough storage available on the device.")
        case .interrupted:
            print("Recording interrupted by another app.")
        case .contentResize:
            print("Recording interrupted by multitasking and content resizing.")
        case .broadcastInvalidSession:
            print("Attempted to start a broadcast without a prior session.")
        case .systemDormancy:
            print("Recording forced to end by the user pressing the power button.")
        case .entitlements:
            print("Recording failed due to missing entitlements.")
        case .activePhoneCall:
            print("Recording unable to record due to active phone call.")
    
        default: break
        }
    }