没有任何委托方法来确定用户选择哪个选项。你必须使用完成处理程序 https://developer.apple.com/documentation/replaykit/rpscreenrecorder/2867291-startcapture and .is麦克风已启用 https://developer.apple.com/documentation/replaykit/rpscreenrecorder/1620987-ismicrophoneenabled以确定所选择的选项。
一旦选择了一个选项completionHandler
叫做:
如果用户选择Don't Allow
那么error
代码将运行
如果用户选择Record Screen & Microphone
then .isMicrophoneEnabled
将被设置为true
如果用户选择Record Screen Only
then .isMicrophoneEnabled
将被设置为false
在 - 的里面completionHandler
您可以检查他们的选择,然后从那时起做您需要做的任何事情。阅读 2 条评论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")
}
})
安全的方法可以让您知道如何应对每个错误代码 https://developer.apple.com/documentation/replaykit/rprecordingerrorcode是对错误代码使用 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
}
}