Why won't the RPScreenRecorder stopRecordingWithHandler work?

I am testing website usability and using WKWebView in my native application. The reason for this is that I can use the COSTouchVisualizer to show strokes, and RPScreenRecorder to record the interaction and loud conversation with the microphone.

I have the following IBAction to start recording:

@IBAction func startRecordSession(sender: AnyObject) { let recorder = RPScreenRecorder.sharedRecorder() guard recorder.available else{ print("Cannot record the screen") return } recorder.delegate = self recorder.startRecordingWithMicrophoneEnabled(true) { (err) in guard err == nil else{ if err!.code == RPRecordingErrorCode.UserDeclined.rawValue{ print("User declined app recording") } else if err!.code == RPRecordingErrorCode.InsufficientStorage.rawValue{ print("Not enough storage to start recording") } else{ print("Error happened = \(err!)") } return } print("Successfully started recording") self.recordBtn.enabled = false self.stopRecordBtn.enabled = true } } 

Which seems to work with printing. Successfully started recording.

However, when the button connected to IBAction is pressed to stop recording, the following code should work:

 @IBAction func stop() { let recorder = RPScreenRecorder.sharedRecorder() print("1. before the recorder function")// This prints recorder.stopRecordingWithHandler{controller, err in guard let previewController = controller where err == nil else { self.recordBtn.enabled = true self.stopRecordBtn.enabled = false print("2. Failed to stop recording")// This does not prints return } previewController.previewControllerDelegate = self self.presentViewController(previewController, animated: true, completion: nil) } } 

But nothing happens except to print the first magazine ("1. in front of the recorder function"). I get no other log entries, and the buttons do not switch their activated status.

I know that IBAction is connected due to a hit statement, but I don't know why I cannot start stopRecordingWithHandler.

I am testing this on an iPad Pro 9.7 "running iOS 9.3.

I'm starting to wonder if it has anything to do with trying to write a WKWebView, but imagine that I get an error if this is a problem.

Any help would be greatly appreciated :)

+5
source share
1 answer

I suspect that if you set a breakpoint in your guard expression (within the stopRecordingCompletionHandler ), it will not crash your program or call the debugger, because the else clause of your guard statement is never called.

This is actually the expected behavior. We did not expect the else clause to be executed if none of the controllers is nil and therefore cannot be bound to the previewController or error constant and therefore is not nil .

With guard only way the else clause will be called is if the specified conditions are NOT true. The print call in closing startRecordingWithMicrophoneEnabled is called because it sits outside the guard statement.

So you just need to move some of this logic from the else clause. However, you still want to handle errors.

 recorder.stopRecordingWithHandler{controller, err in guard let previewController = controller where err == nil else { print("2. Failed to stop recording with error \(err!)") // This prints if there was an error return } } self.recordBtn.enabled = true self.stopRecordBtn.enabled = false previewController.previewControllerDelegate = self self.presentViewController(previewController, animated: true, completion: nil) print("stopped recording!") 

Guard

Therefore, to be sure that we are sitting in the guard syntax, this is:

 guard <condition> else { <statements to execute if <condition> is not true> } 

In your example, you combine guard with an optional binding and a where clause to create the following situation:

  • If the controller not nil , it will be bound to a constant called previewController .
  • If it is nil , then we stop evaluating, never create the previewController constant previewController and don’t move on to the else clause, which should pass control using a keyword such as return .
  • If controller not nil and our constant was created, we will continue and consider our where clause.
  • If where evaluates to true (therefore, if there is no error), we will pass the test and execute the guard statement. else will never be executed.
  • If where evaluates to false , we will execute the else block and , after the guard statement is not called.
+1
source

All Articles