Last active
February 25, 2019 07:32
-
-
Save kboy-silvergym/186dc97d2d9a2e6c7c70edc1dbac963a to your computer and use it in GitHub Desktop.
Sample code for ReplayKit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ReplayKit | |
func startRecording() { | |
let recorder = RPScreenRecorder.shared() | |
recorder.isMicrophoneEnabled = true | |
recorder.startRecording(handler: { error in | |
if let error = error { | |
NSLog("Failed start recording: \(error.localizedDescription)") | |
return | |
} | |
NSLog("Start recording") | |
}) | |
} | |
func finishRecordingAndShowPreview() { | |
guard RPScreenRecorder.shared().isRecording else { | |
return | |
} | |
RPScreenRecorder | |
.shared() | |
.stopRecording(handler: { [weak self] (previewViewController, error) in | |
guard let self = self, let previewViewController = previewViewController else { | |
return | |
} | |
if let error = error { | |
NSLog("Failed stop recording: \(error.localizedDescription)") | |
return | |
} | |
DispatchQueue.main.async { | |
previewViewController.previewControllerDelegate = self | |
previewViewController.modalPresentationStyle = .overCurrentContext | |
self.present(previewViewController, animated: true, completion: nil) | |
} | |
}) | |
} | |
extension ViewController: RPPreviewViewControllerDelegate { | |
func previewControllerDidFinish(_ previewController: RPPreviewViewController) { | |
DispatchQueue.main.async { | |
previewController.dismiss(animated: true, completion: nil) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment