我正在使用 RPScreenRecorder 来录制屏幕。当录制开始时,RPScreenRecorder 会向用户显示一个权限请求警报对话框。然而,似乎没有简单的方法来检测用户是否拒绝权限请求。以下是我的代码片段。
@objc func startRecording(width: Int32, height: Int32) -> Bool {
var res : Bool = true
.......
if #available(iOS 11.0, *) {
recorder.startCapture(handler:
(cmSampleBuffer, rpSampleType, error) in guard error == nil else {
if let rpError = error as NSError?, rpError.code == -5801 {
// if let rpError = error as NSError?, rpError.code == PRRecordingErrorCode.userDeclined.rawValue //The user declined application recording permission
print("User declined application recording permission.")
res = false
}
return;
}
return Bool(res)
}
错误代码-5801似乎没有填充在
startRecording
下。感谢任何反馈。
startCapture
方法需要两个块:handler
和completion
。完成块提供了一个可选的 Error
参数。如果用户拒绝录制,则会调用完成块,并显示一个表示拒绝的错误。
可以使用类似于以下的代码来检查这一点:
recorder.startCapture(handler: { buffer, type, error in
print("capture started")
if let error {
// Not reached when the user denies the recording
print("capture start error: \(error)")
}
}) { error in
print("capture completion")
if let error = error as? NSError, let code = RPRecordingErrorCode(rawValue: error.code) {
switch code {
case .userDeclined:
print("User declined")
default:
print("capture completion error: \(error)")
}
}
}
RPRecordingErrorCode
。