ios - ReplayKit 初始警报选择的委托(delegate)方法是什么?

标签 ios swift replaykit

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

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

有哪些委托(delegate)方法可以帮助我找出用户选择的选项?

enter image description here

最佳答案

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

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

  1. 如果用户选择不允许,则将运行错误代码

  2. 如果用户选择录制屏幕和麦克风,则 .isMicrophoneEnabled 将设置为 true

  3. 如果用户选择仅录制屏幕,则.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
    }
}

关于ios - ReplayKit 初始警报选择的委托(delegate)方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59495052/

相关文章:

swift Alamofire 帖子 "Extra argument in call"

swift - 如何修复复制/粘贴 Xcode 时损坏的缩进

swift - 为什么实例方法在 Swift 中有这种类型?

ios - SCNPhysicsBody 表现不佳

ios - RPScreenRecorder stopRecording block 未被调用

ios - Replaykit 生成日志 "The operation couldn’ 尚未完成。 (com.apple.ReplayKit.RPRecordingErrorDomain 错误 -5803。)”开始录制

ios - RPPreviewViewController 在播放后禁用音频

ios - 有没有一种简单的方法可以获取当前最流行的 SoundCloud 轨道?

iOS react native : Add build configuration other than Debug and Release

ios - 设备 ID 未存储在服务器 IOS7 中