UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How to record user videos using ReplayKit

Swift version: 5.6

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.
Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Available from iOS 9.0

Similar solutions…

About the Swift Knowledge Base

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

BUY OUR BOOKS
Buy Pro Swift Buy Pro SwiftUI Buy Swift Design Patterns Buy Testing Swift Buy Hacking with iOS Buy Swift Coding Challenges Buy Swift on Sundays Volume One Buy Server-Side Swift Buy Advanced iOS Volume One Buy Advanced iOS Volume Two Buy Advanced iOS Volume Three Buy Hacking with watchOS Buy Hacking with tvOS Buy Hacking with macOS Buy Dive Into SpriteKit Buy Swift in Sixty Seconds Buy Objective-C for Swift Developers Buy Beyond Code

Was this page useful? Let us know!

Average rating: 4.0/5

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.