Created
March 22, 2023 10:26
-
-
Save Joony/210e2eb9d25e6026ef3da68038e1f4eb to your computer and use it in GitHub Desktop.
A SwiftUI model for recording the screen.
This file contains hidden or 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 | |
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