当用户第一次决定使用ReplayKit时,会出现一个警报。它有三个选择:
-Record Screen and Microphone
-Record Screen Only
-Don’t Allow
什么是委托方法,以便我可以找出用户选择哪个选项?
发布于 2020-01-04 16:16:30
没有任何委托方法来确定用户选择哪个选项。您必须使用completionHandler和.isMicrophoneEnabled来确定所选选项。
一旦选择了一个选项,就会调用completionHandler
:
Don't Allow
,那么error
代码将运行Record Screen & Microphone
,那么.isMicrophoneEnabled
将被设置为true
Record Screen Only
,那么.isMicrophoneEnabled
将被设置为false
在completionHandler
中,您可以查看他们的选择,然后从那时起做您需要做的任何事情。阅读下面代码的completionHandler
部分中的两个注释。
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")
}
})
为了让您知道如何响应每个错误码,安全的方法是对错误代码使用开关语句:
}, 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
}
}
发布于 2022-04-27 12:32:24
如果您只想检测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")
}
}
}
https://stackoverflow.com/questions/59495052
复制相似问题