0

enter image description here

recently i found when user select the last one item [dont allow],but didnt have any callback tell me ,cause the developer don`t konw which one user select it.

static private func startCapture(callback:@escaping(_ code:ScreenCheckStatus,_ msg:String) ->Void) {
        RPScreenRecorder.shared().isMicrophoneEnabled = true
        isCheckAuthAlreadStop = false
        startListenRefuseRecord(callback: callback)
        ///startCapture(handler captureHandler: ((CMSampleBuffer, RPSampleBufferType, Error?) -> Void)?) async throws
        RPScreenRecorder.shared().startCapture { buffer, bufType, error in
            if error != nil {
                callback(.startRecordError,error!.localizedDescription)
            }else{
                self.stopListenRefuseRecord()
                if buffer.isValid && CMSampleBufferDataIsReady(buffer) == true && !self.isCheckAuthAlreadStop{
                    self.stopCapture(callback: callback)
                    self.isCheckAuthAlreadStop = true
                }
            }
        }
    }

    static private func startListenRefuseRecord(callback:@escaping(_ code:ScreenCheckStatus,_ msg:String) ->Void) {
        ScreenCheck.isUserRefuseRecord = false
        let publisher1 = RPScreenRecorder.shared().publisher(for: \.isRecording)
        let publisher2 = RPScreenRecorder.shared().publisher(for: \.isMicrophoneEnabled)
        let cancelBag = Subscribers.Sink<Bool, Never>(receiveCompletion: { complete in
        }, receiveValue: {value in
            if !ScreenCheck.isUserRefuseRecord && value{
                ScreenCheck.isUserRefuseRecord = true
                callback(.resufeRecordError,"用户拒绝屏幕录制,请重新启动app,否则将导致屏幕录制失败")
                ScreenCheck.stopListenRefuseRecord()
            }
        })
        let latest = Publishers.CombineLatest(publisher1, publisher2)
            .throttle(for: 2, scheduler: RunLoop.main, latest: true)
            .map{ !$0 && !$1}
        latest.subscribe(cancelBag)
        self.userRefuseRecordCancelBag = cancelBag
    }

how to konw why the replaykit don`t give callback when use select limits of authority ,and how to solve this problem ?

boy lucky
  • 26
  • 1
  • 1
    You should get an `error` value of [`userDeclined`](https://developer.apple.com/documentation/replaykit/rprecordingerrorcode/userdeclined) – Paulw11 Jul 21 '23 at 05:40

1 Answers1

0

If the user declines screen recording then you will get a userDeclined error passed to your handler.

RPScreenRecorder.shared().startRecording() { error in
   if let error = error as? NSError {
      if error.domain == RPRecordingErrorDomain && error.code == RPRecordingErrorCode.userDeclined.rawValue {
          print("User declined screen recording")
      } else {
          print(error)
      }
    } else {
        print("No error")
    }
}

With startCapture the code is very similar; The error is reported to the completionHandler closure:

RPScreenRecorder.shared().startCapture( handler: { buffer, bufferType, error in
            //...
    },
    completionHandler: { error in
        if let error = error as? NSError {
            if error.domain == RPRecordingErrorDomain && error.code == RPRecordingErrorCode.userDeclined.rawValue {
               print("User declined screen recording")
            } else {
               print(error)
            }
       } else {
            print("No error")
       }
   })
Paulw11
  • 108,386
  • 14
  • 159
  • 186
  • I remember when the user doesn't allow it, there will be an error code, but now don`t have any callback, u can try it – boy lucky Jul 21 '23 at 06:47
  • i mean use "startCapture" api – boy lucky Jul 21 '23 at 07:00
  • 1
    You have to provide the `completionHandler` closure. See my update – Paulw11 Jul 21 '23 at 09:19
  • thank u , as same as your say i try to test the code , the refuse callback in the completionHandler closure。Then you try to refuse again , don`t have any responce – boy lucky Jul 31 '23 at 06:26
  • In my testing the `completionHandler` was called with an error each time I tried to start capture and the user declined. – Paulw11 Jul 31 '23 at 07:14
  • haha, ok for me i`m also tried to test replaykit,maybe we meet different situation,anyway thank u friendly help – boy lucky Jul 31 '23 at 08:12