ReplayKit 初始警报选择的委托方法是什么?

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

当用户第一次决定使用 ReplayKit 时,会出现一个警告。它给出了 3 个选择:

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

有哪些委托方法可以让我找出用户选择的选项?

没有任何委托方法来确定用户选择哪个选项。您必须使用 completionHandler and .isMicrophoneEnabled 来确定所选择的选项。

一旦选择了一个选项,completionHandler 就会被调用:

  1. 如果用户选择 Don't Allow 那么 error 代码将 运行

  2. 如果用户选择 Record Screen & Microphone 那么 .isMicrophoneEnabled 将设置为 true

  3. 如果用户选择 Record Screen Only 那么 .isMicrophoneEnabled 将设置为 false

completionHandler 中,您可以查看他们的选择,然后从那时起做您需要做的任何事情。阅读下面代码 completionHandler 部分的 2 条评论。

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")
    }
})

执行此操作以便您知道如何响应 each error code 的安全方法是对错误代码使用 switch 语句:

}, 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
    }
}

如果您只想检测 Don't Allow 何时被点击,这里有一个简单的解决方案:

recorder.startCapture(handler: { [self] (sampleBuffer, sampleType, passedError) in
        if let passedError = passedError {
            print(passedError.localizedDescription)
            return
        }
            
        }) { err in
            if let error = err {
                if error._code == RPRecordingErrorCode.userDeclined.rawValue {
                    print("User didn't allow recording")
                }
            }
        }