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

How to record user videos using ReplayKit

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() {
        super.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 {
                print(unwrappedError.localizedDescription)
            } 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.

Upgrade to the premium experience

Get all 40 projects in PDF and ePub, plus exclusive content that will take your Swift learning to the next level – buy the Hacking with Swift book today!

MASTER SWIFT NOW
Buy Practical iOS 12 Buy Pro Swift Buy Swift Design Patterns Buy Practical iOS 11 Buy Swift Coding Challenges Buy Server-Side Swift (Vapor Edition) Buy Server-Side Swift (Kitura Edition) Buy Hacking with macOS Buy Advanced iOS Volume One Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with Swift Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let me know!

Click here to visit the Hacking with Swift store >>