Skip to content

Instantly share code, notes, and snippets.

@Joony
Created March 22, 2023 10:26
Show Gist options
  • Save Joony/210e2eb9d25e6026ef3da68038e1f4eb to your computer and use it in GitHub Desktop.
Save Joony/210e2eb9d25e6026ef3da68038e1f4eb to your computer and use it in GitHub Desktop.
A SwiftUI model for recording the screen.
import ReplayKit
class ScreenRecorderModel {
private let recorder = RPScreenRecorder.shared()
var isAvailable: Bool { recorder.isAvailable }
func start(handler: ((Error?) -> Void)? = nil) {
recorder.startRecording(handler: handler)
}
func save(id: String) {
guard recorder.isRecording else { return }
let paths = NSSearchPathForDirectoriesInDomains(.cachesDirectory, .userDomainMask, true)
guard let dirPath = paths.first else { return }
var dirUrl = URL(fileURLWithPath: dirPath)
dirUrl.append(path: "ScreenRecordings")
if !FileManager.default.fileExists(atPath: dirUrl.path) {
guard (try? FileManager.default.createDirectory(atPath: dirUrl.path, withIntermediateDirectories: true)) != nil else { return }
}
dirUrl.appendPathComponent(id + ".mp4")
RPScreenRecorder.shared().stopRecording(withOutput: dirUrl) { error in
print("done")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment