coding22
coding22

Reputation: 689

Why cant I dismiss the replaykit previewController in Swift?

I used this code in swift 2 and it worked. But now in Swift 3 the preview Controller window that pops up when the recording is finish doesn't dismiss when I press the cancel or save button. What am I doing wrong?

func stopRecording() {

let sharedRecorder = RPScreenRecorder.shared()
sharedRecorder.stopRecording(handler: { (previewController: RPPreviewViewController?, error) in

if previewController != nil {
            print("stopped recording")

                self.previewViewController.previewControllerDelegate = self
                self.view?.window?.rootViewController?.present(previewController!, animated: true, completion: nil)
 }



func previewControllerDidFinish(previewController: RPPreviewViewController) {

previewController.dismiss(animated: true, completion: nil)

}

Upvotes: 0

Views: 722

Answers (2)

Swifty Codes
Swifty Codes

Reputation: 1102

//Try this code hope it helps:

func startRecording() {
    let recorder = RPScreenRecorder.shared()

    if #available(iOS 9.0, *) {
        recorder.startRecording(withMicrophoneEnabled: true) { [unowned self] (error) in
            if let unwrappedError = error {
                print(unwrappedError.localizedDescription)
            } else {
                self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Stop", style: .plain, target: self, action: #selector(PreviewVC.stopRecording))
            }
        }
    } else {
        // Fallback on earlier versions
    }
}

func stopRecording() {
    let recorder = RPScreenRecorder.shared()

    recorder.stopRecording { [unowned self] (preview, error) in
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Start", style: .plain, target: self, action: #selector(PreviewVC.startRecording))

        if let unwrappedPreview = preview {
            unwrappedPreview.previewControllerDelegate = self
            self.present(unwrappedPreview, animated: true, completion: nil)
        }
    }
}

func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
    self.dismiss(animated: true, completion: nil)
}

Upvotes: 1

Han-byul Yoo
Han-byul Yoo

Reputation: 21

You should change the last line from:

previewController.dismiss(animated: true, completion: nil)

to:

dismiss(animated: true, completion: nil)

Upvotes: 1

Related Questions