You can use ReplayKit to record your screen. It's very easy to use, and it's also very easy to use it on the mic. and so on. This time, I saved it as a video, but it can be used for many things, such as combining it with WebRTC.
This file contains 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 UIKit | |
import ReplayKit | |
class ViewController: UIViewController { | |
let recorder = RPScreenRecorder.shared() | |
var iconImageView = UIImageView() | |
override func viewDidLoad() { | |
super.viewDidLoad() | |
iconImageView.image = UIImage(named: "icon") | |
iconImageView.frame = CGRect(x: 100, y: 100, width: 100, height: 100) | |
view.addSubview(iconImageView) | |
let startRecordButton = UIButton() | |
startRecordButton.setTitle("Start", for: .normal) | |
startRecordButton.backgroundColor = .lightGray | |
startRecordButton.frame = CGRect(x: view.bounds.size.width - 200, y: view.bounds.size.height - 100, width: 60, height: 60) | |
startRecordButton.addTarget(self, action: #selector(startRecording), for: .touchDown) | |
view.addSubview(startRecordButton) | |
let stopRecordButton = UIButton() | |
stopRecordButton.setTitle("Stop", for: .normal) | |
stopRecordButton.backgroundColor = .lightGray | |
stopRecordButton.frame = CGRect(x: view.bounds.size.width - 100, y: view.bounds.size.height - 100, width: 60, height: 60) | |
stopRecordButton.addTarget(self, action: #selector(startRecording), for: .touchDown) | |
view.addSubview(stopRecordButton) | |
recorder.isMicrophoneEnabled = true | |
} | |
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let touch = touches.first { | |
let point = touch.location(in: self.view) | |
iconImageView.center = point | |
} | |
} | |
override open func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let touch = touches.first { | |
let point = touch.location(in: self.view) | |
iconImageView.center = point | |
} | |
} | |
override open func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { | |
if let touch = touches.first { | |
let point = touch.location(in: self.view) | |
iconImageView.center = point | |
} | |
} | |
@objc func startRecording() { | |
recorder.startRecording { (error) in | |
if let error = error { | |
print(error) | |
} | |
} | |
} | |
@objc func endRecording() { | |
recorder.stopRecording { (previewVC, error) in | |
if let previewVC = previewVC { | |
previewVC.previewControllerDelegate = self | |
self.present(previewVC, animated: true) | |
} | |
if let error = error { | |
print(error) | |
} | |
} | |
} | |
} | |
extension UIViewController: RPPreviewViewControllerDelegate { | |
public func previewControllerDidFinish(_ previewController: RPPreviewViewController) { | |
dismiss(animated: true, completion: nil) | |
} | |
} |