NEW: Master Swift design patterns with my latest book! >>

How to record user videos using ReplayKit

Written by Paul Hudson    @twostraws

ReplayKit is one of many useful social media features built into iOS, and it's trivial to add to your projects. What's more, it's not just for games – you can record any kind of app just fine. I should add, though, that the recording quality is fairly low, so it's not worth trying to record fine details.

Below is a complete example of how to use ReplayKit to record the screen. You'll need to add a navigation controller around your view controller because the code uses the right bar button item to start and stop recording, and of course it's down to you to add some interesting user interface that's actually worth recording!

import ReplayKit
import UIKit

class ViewController: UIViewController, RPPreviewViewControllerDelegate {
    override func viewDidLoad() {

        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Start", style: .plain, target: self, action: #selector(startRecording))

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

        recorder.startRecording{ [unowned self] (error) in
            if let unwrappedError = error {
            } else {
                self.navigationItem.rightBarButtonItem = UIBarButtonItem(title: "Stop", style: .plain, target: self, action: #selector(self.stopRecording))

    @objc 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(self.startRecording))

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

    func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
        dismiss(animated: true)

ReplayKit does three more cool things for you:

  • It automatically asks the user for permission to start recording the first time you try.
  • While recording, no system user interface is shown, which means you can't see messages coming in from other apps.
  • When the user stops recording, they'll get the chance to preview their recording, make changes, save it to their devices and share it – your app doesn't get access to the recording.

Available from iOS 9.0

Did this solution work for you? Please pass it on!

Other people are reading…

About the Swift Knowledge Base

This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.

Download for free!

Want a free 75-minute video teaching functional programming, protocol-oriented programming, and more? This is your lucky day!

Click here to visit the Hacking with Swift store >>