首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >ReplayKit的初始警报选择的委托方法是什么?

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

Stack Overflow用户
提问于 2019-12-27 01:26:54
回答 2查看 674关注 0票数 2

当用户第一次决定使用ReplayKit时,会出现一个警报。它有三个选择:

代码语言:javascript
运行
复制
-Record Screen and Microphone
-Record Screen Only
-Don’t Allow

什么是委托方法,以便我可以找出用户选择哪个选项?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-04 16:16:30

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

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

  1. 如果用户选择Don't Allow,那么error代码将运行
  2. 如果用户选择Record Screen & Microphone,那么.isMicrophoneEnabled将被设置为true
  3. 如果用户选择Record Screen Only,那么.isMicrophoneEnabled将被设置为false

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

代码语言:javascript
运行
复制
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")
    }
})

为了让您知道如何响应每个错误码,安全的方法是对错误代码使用开关语句:

代码语言:javascript
运行
复制
}, 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
    }
}
票数 2
EN

Stack Overflow用户

发布于 2022-04-27 12:32:24

如果您只想检测Don't Allow何时被窃听,下面是一个简单的解决方案:

代码语言:javascript
运行
复制
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")
                }
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59495052

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档