I have created BaseViewController to use as subclass for all of my view controllers so that I can show alert whenever I need any progess to show and hide which is lazy variable.
Everything is cool until now. But I figured out that all my viewcontroller which are inherting from this are not releasing. What is the problem?
class BaseViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
self.view.backgroundColor = UIColor(red: 0.4 / 255.0, green: 100 / 215.0, blue: 120 / 255.0, alpha: 1.0)
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return preferredStatusBarStyle_Internal()
}
override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
return supportedInterfaceOrientations_Internal()
}
lazy var progressHUD: MBProgressHUD = {
if let navController = self.navigationController {
return navController.HUD
}
return self.HUD
}()
func showAlert(_ title: String?, message: String) {
let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .cancel, handler: { _ in
DispatchQueue.main.async {
self.progressHUD.hide(animated: false, afterDelay: 1.0)
}
})
alert.addAction(action)
present(alert, animated: true, completion: nil)
}
func showPermissionDeniedAlert(_ message: String) {
let alertController = UIAlertController(title: message, message: "Go to Settings?".localized, preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings".localized, style: .default) { _ in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
if #available(iOS 10.0, *) {
UIApplication.shared.open(settingsUrl, completionHandler: { success in
print("Settings opened: \(success)") // Prints true
})
} else {
let success = UIApplication.shared.openURL(settingsUrl)
print("Settings opened: \(success)")
}
}
}
alertController.addAction(settingsAction)
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
present(alertController, animated: true, completion: nil)
}
}
extension UIViewController {
func preferredStatusBarStyle_Internal() -> UIStatusBarStyle {
return .lightContent
}
func supportedInterfaceOrientations_Internal() -> UIInterfaceOrientationMask {
return isiPad() ? .allButUpsideDown : .all
}
var HUD: MBProgressHUD {
let progressHUD = MBProgressHUD(viewController: self)
return progressHUD
}
}
If the view controller is not releasing it means you are creating retain cycle.
It could be in two places
pass weakself to async block.
DispatchQueue.main.async { [weak self] in
self?.progressHUD.hide(animated: false, afterDelay: 1.0)
}
passing a weak reference if MBProgressHUD init is creating retain cycle
var HUD: MBProgressHUD {
// you can also use weak self
unowned let unownedSelf = self
let progressHUD = MBProgressHUD(viewController: unownedSelf)
return progressHUD
}
Related
I am trying to use ReplayKit to record my app, and then give the user the option to share or delete the recording. The deleting works fine, but I can't get the sharing part to work properly. I've tried to use RPPreviewViewControllerMode with .shared, but XCode doesn't recognize it. I've also tried UIActivityViewController, which does pop-up the sharing menu - however, it doesn't work if I try to access the recording. Lastly, I tried previewControllerDelegate which allows the user to edit and save the video, but not share it.
I've posted my code below. Please advise, thank you!
import UIKit
import ReplayKit
class ViewController: UIViewController, RPPreviewViewControllerDelegate {
#IBOutlet weak var statusLabel: UILabel!
#IBOutlet weak var imagePicker: UISegmentedControl!
#IBOutlet weak var selectedImageView: UIImageView!
#IBOutlet weak var micToggle: UISwitch!
#IBOutlet weak var recordButton: UIButton!
var recorder = RPScreenRecorder.shared()
private var isRecording = false
#IBAction func imagePicked(_ sender: UISegmentedControl) {
switch sender.selectedSegmentIndex {
case 0:
selectedImageView.image = UIImage(named: "skate")
case 1:
selectedImageView.image = UIImage(named: "food")
case 2:
selectedImageView.image = UIImage(named: "cat")
case 3:
selectedImageView.image = UIImage(named: "nature")
default:
selectedImageView.image = UIImage(named: "skate")
}
}
#IBAction func recordButtonPressed(_ sender: Any) {
if !isRecording {
startRecording()
} else {
stopRecording()
}
}
func startRecording() {
guard recorder.isAvailable else {
print("Recording not available at this time")
return
}
if micToggle.isOn {
recorder.isMicrophoneEnabled = true
} else {
recorder.isMicrophoneEnabled = false
}
recorder.startRecording { (error) in
guard error == nil else {
print("There was an error startng the recording.")
return
}
//Call DispatchQueue to update UI in the main thread rather than background
DispatchQueue.main.async {
self.micToggle.isEnabled = false
self.recordButton.setTitleColor(#colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1), for: .normal)
self.recordButton.setTitle("Stop", for: .normal)
self.statusLabel.textColor = #colorLiteral(red: 0.521568656, green: 0.1098039225, blue: 0.05098039284, alpha: 1)
self.statusLabel.text = "Recording..."
self.isRecording = true
print("Started Recording")
}
}
}
func stopRecording() {
recorder.stopRecording { (preview, error) in
guard preview != nil else {
print("Preview controller not available")
return
}
let alert = UIAlertController(title: "Recording finished", message: "Would you like to share or delete your recording?", preferredStyle: .alert
)
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action) in
self.recorder.discardRecording {
print("Recording discarded successfully.")
}
})
//First try:
let recordedVideo = "Video goes here!" //I don't know how to modify this to get the video content.
let activityViewController : UIActivityViewController = UIActivityViewController(
activityItems: [recordedVideo], applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil)
//Second try:
//This allows me to edit and save the video, but not share it.
let shareAction = UIAlertAction(title: "Share", style: .default, handler: { (action) in
preview?.previewControllerDelegate = self
self.present(preview!, animated: true, completion: nil)
})
// //Third try: not working!
// var mode: RPPreviewViewControllerMode
// let shareAction = UIAlertAction(title: "Share", style: .default, handler: { (action) in
// preview?.mode = .share // The error is: 'mode' has been explicitly marked unavailable here (ReplayKit.RPPreviewViewController)
// preview?.previewControllerDelegate = self
// self.present(preview!, animated: true, completion: nil)
// })
alert.addAction(deleteAction)
alert.addAction(shareAction)
self.present(alert, animated: true, completion: nil)
self.isRecording = false
self.viewReset()
}
}
func viewReset() {
micToggle.isEnabled = true
statusLabel.text = "Ready to Record"
statusLabel.textColor = #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
recordButton.setTitle("Record", for: .normal)
recordButton.setTitleColor(#colorLiteral(red: 0.2994912565, green: 0.7500386834, blue: 0.3387371898, alpha: 1), for: .normal)
}
func previewControllerDidFinish(_ previewController: RPPreviewViewController) {
dismiss(animated: true, completion: nil)
}
}
Maybe it is because of .automatic modal presentation 'cause in iOS 13 it doesn't cover full screen when you popup your controller. So, try .fullScreen, it helps me:
func stopRecording() {
recorder.stopRecording { (preview, error) in
guard preview != nil else {
print("Preview controller not available")
return
}
let alert = UIAlertController(title: "Recording finished", message: "Would you like to share or delete your recording?", preferredStyle: .alert
)
let deleteAction = UIAlertAction(title: "Delete", style: .destructive, handler: { (action) in
self.recorder.discardRecording {
print("Recording discarded successfully.")
}
})
let shareAction = UIAlertAction(title: "Share", style: .default, handler: { (action) in
// Try .fullScreen
preview?.modalPresentationStyle = .fullScreen
preview?.previewControllerDelegate = self
self.present(preview!, animated: true, completion: nil)
})
alert.addAction(deleteAction)
alert.addAction(shareAction)
self.present(alert, animated: true, completion: nil)
self.isRecording = false
self.viewReset()
}
}
I am working with Coordinators.
My ViewController is not deallocating even though I set weak delegates.
Coordinator:
class JournalDetailCoordinator: Coordinator {
var dependencys: AppDependency
var navigationController: UINavigationController
var collectionViewController: CollectionViewWithMenuController!
var imagePickerManager: ImagePickerManager!
init(dependencys: AppDependency, navigationController: UINavigationController) {
self.dependencys = dependencys
self.navigationController = navigationController
}
func start() {
loadCollectionViewController()
}
deinit {
print("JournalDetailCoordinator deinitialisiert")
}
func loadCollectionViewController() {
var journalDetailViewControllerContainer = [JournalDetailViewController]()
for journal in dependencys.journals {
let vc: JournalDetailViewController = dependencys.getJournalDetailDependency().createVC()
vc.entryJournal = journal
vc.delegateLoadImagePickerManager = self
journalDetailViewControllerContainer.append(vc)
}
collectionViewController = dependencys.getCollectionViewWithMenuDependency().createVC()
collectionViewController.managedViewControllers = journalDetailViewControllerContainer
navigationController.pushViewController(collectionViewController, animated: true)
}
}
extension JournalDetailCoordinator: LoadImagePickerManager {
func loadImagePickerManager<T>(vc: T) where T : UIViewController & ImageGetterDelegate {
imagePickerManager = ImagePickerManager()
imagePickerManager.delegate = vc
imagePickerManager.pickImage(viewController: collectionViewController)
}
}
ViewController:
class JournalDetailViewController: UIViewController {
lazy var mainView: JournalDetailViewP = {
let view = JournalDetailViewP()
return view
}()
typealias myType = SetJournal & HasImagePickerManager
// dependency
var dep: myType!
var entryJournal: Journaling!
var tableViewDataSource: JournalDetailTVDataSource?
var collectionViewInteraction: AddImageCollectionViewInteraction?
weak var delegateLoadImagePickerManager: LoadImagePickerManager?
override func viewDidLoad() {
super.viewDidLoad()
title = "Detail Journal"
// only for testing without coordinator connection
// if entryJournal == nil {
// entryJournal = NewJournal()
// }
// dep = AppDependency()
setMainView()
loadTableView()
loadCollectionView()
}
override func viewDidDisappear(_ animated: Bool) {
print("view did disappear Journal Detail")
}
deinit {
dep.setJournal(newJournal: entryJournal)
print("JournalDetailViewController deinitialisiert")
}
#objc func getImage() {
delegateLoadImagePickerManager?.loadImagePickerManager(vc: self)
// dep.imagePickerManager.delegate = self
// dep.imagePickerManager.pickImage(viewController: self)
}
func saveEntry() {
}
}
extension JournalDetailViewController: Storyboarded {}
extension JournalDetailViewController: DependencyInjectionVC {}
extension JournalDetailViewController: SetMainView {}
extension JournalDetailViewController: ImageGetterDelegate {
func returnImage(image: UIImage) {
if entryJournal.image[0] == nil {
entryJournal.image[0] = image
} else {
entryJournal.image.append(image)
}
loadCollectionView()
}
}
extension JournalDetailViewController: AddImageCollectionViewInteractionDelegate {
func deleteImage(index: Int) {
}
func addImage() {
getImage()
}
}
They are deallocation if I do not execute the getImage() function, so I think that is the reason of the retention circle.
Thats the ImagePickerManager:
protocol ImageGetterDelegate: class {
func returnImage(image: UIImage)
}
class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
weak var delegate: ImageGetterDelegate?
override init() {
super.init()
print("ImagePickerManager initialisiert")
}
deinit {
print("imagePickerManager deinitialisiert")
}
/// use to pick the Image, make sure to use the root ViewController to pass in to
func pickImage<T:UIViewController>(viewController: T) {
let alertList = UIAlertController(title: NSLocalizedString("Load Picture", comment: "Picture alert Alertcontroller"), message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) {
UIAlertAction in self.openCamera(viewController: viewController)
alertList.dismiss(animated: true, completion: nil)
}
let galleryAction = UIAlertAction(title: "Gallery", style: .default) {
UIAlertAction in self.openGallery(viewController: viewController)
alertList.dismiss(animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
UIAlertAction in
alertList.dismiss(animated: true, completion: nil)
}
alertList.addAction(cameraAction)
alertList.addAction(galleryAction)
alertList.addAction(cancelAction)
viewController.present(alertList, animated: true, completion: nil)
}
private func openCamera<T:UIViewController>(viewController: T) {
if(UIImagePickerController .isSourceTypeAvailable(.camera)) {
imagePicker.sourceType = .camera
imagePicker.delegate = self
viewController.present(imagePicker, animated: true, completion: nil)
} else {
let warningAlert = UIAlertController(title: "Warning", message: "You do not have a camera", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Okay", style: .cancel) {
UIAlertAction in
warningAlert.dismiss(animated: true, completion: nil)
}
warningAlert.addAction(cancelAction)
viewController.present(warningAlert, animated: true, completion: nil)
}
}
private func openGallery<T:UIViewController>(viewController: T) {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
viewController.present(imagePicker, animated: true, completion: nil)
}
#objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[.originalImage] as? UIImage else {
print("Expected a dictionary containing an image, but was provided the following: \(info)")
return
}
delegate?.returnImage(image: image)
}
}
ImagePickerManager is not allocating after the Coordinator is deallocated. So I think the Retention circle is because I pass the ViewVontroller back through to the Coordinator in LoadImagePickerManager and then set the vc to the Coordinator? Does anybody have an idea how to solve that problem or what to do?
Edit:
LoadImagePickerManager:
protocol LoadImagePickerManager: class {
func loadImagePickerManager<T: UIViewController & ImageGetterDelegate>(vc: T)
}
I think the memory leak happens here when passing the collectionViewController:
imagePickerManager.pickImage(viewController: collectionViewController)
Because I did some tests if I do not execute this part then everything is deallocating fine.
Updated ImagePickerManager class:
class ImagePickerManager: NSObject, UIImagePickerControllerDelegate, UINavigationControllerDelegate {
var imagePicker = UIImagePickerController()
weak var delegate: ImageGetterDelegate?
var viewController: UIViewController!
override init() {
super.init()
print("ImagePickerManager initialisiert")
}
deinit {
print("imagePickerManager deinitialisiert")
}
/// use to pick the Image, make sure to use the root ViewController to pass in to
func pickImage<T:UIViewController>(viewController: T) {
self.viewController = viewController
let alertList = UIAlertController(title: NSLocalizedString("Load Picture", comment: "Picture alert Alertcontroller"), message: nil, preferredStyle: .actionSheet)
let cameraAction = UIAlertAction(title: "Camera", style: .default) {
UIAlertAction in self.openCamera()
alertList.dismiss(animated: true, completion: nil)
}
let galleryAction = UIAlertAction(title: "Gallery", style: .default) {
UIAlertAction in self.openGallery()
alertList.dismiss(animated: true, completion: nil)
}
let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) {
UIAlertAction in
alertList.dismiss(animated: true, completion: nil)
}
alertList.addAction(cameraAction)
alertList.addAction(galleryAction)
alertList.addAction(cancelAction)
viewController.present(alertList, animated: true, completion: nil)
}
private func openCamera() {
if(UIImagePickerController .isSourceTypeAvailable(.camera)) {
imagePicker.sourceType = .camera
imagePicker.delegate = self
viewController.present(imagePicker, animated: true, completion: nil)
} else {
let warningAlert = UIAlertController(title: "Warning", message: "You do not have a camera", preferredStyle: .alert)
let cancelAction = UIAlertAction(title: "Okay", style: .cancel) {
UIAlertAction in
warningAlert.dismiss(animated: true, completion: nil)
}
warningAlert.addAction(cancelAction)
viewController.present(warningAlert, animated: true, completion: nil)
}
}
private func openGallery() {
imagePicker.sourceType = .photoLibrary
imagePicker.delegate = self
viewController.present(imagePicker, animated: true, completion: nil)
}
#objc func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
picker.dismiss(animated: true, completion: nil)
guard let image = info[.originalImage] as? UIImage else {
print("Expected a dictionary containing an image, but was provided the following: \(info)")
return
}
viewController = nil
delegate?.returnImage(image: image)
}
}
I added a viewController variable to the class, and set it through the pickImage() and then when the image is selected I set the variable to nil. Then the UIViewController gets deallocated, but still the class ImagePickerManager stays alive and does not get allocated.
Since you are using weak delegate so, in no way it is going to create a retain cycle.
I think your viewController is not deallocating because your viewController is still in the stack of your navigation.
Try to remove all the viewControllers from the navigation stack and then your deallocate block will work as usual.
Try the following code depending upon your requirement(present/push) when you are coming back to your homeViewController:
self.navigationController?.popToRootViewController(animated: true)
self.view.window?.rootViewController?.dismiss(animated: true, completion: nil)
Edit:
Make sure your protocol is a class type then only weak reference will work.
protocol LoadImagePickerManager: class {
}
In your PickerManager try to dismiss using following code, it will redirect you to rootview controller, but you can again push or present to the required viewcontroller:
self.view.window?.rootViewController?.dismiss(animated: false, completion: nil)
I'm trying to implement a UINavigationBarDelegate, and I want to show a alert to let user determine whether leave this view.
This is my code:
extension CDFFormController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if let entityViewController = self.topViewController as? MyEntityViewController {
if entityViewController.isEditing {
let semaphore = DispatchSemaphore(value: 0)
var result = false
let alert = UIAlertController(title: "Leave the view?", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "leave", style: .default, handler: { _ in
result = true
semaphore.signal()
}))
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: { _ in
semaphore.signal()
}))
entityViewController.present(alert, animated: true, completion: nil)
semaphore.wait()
return result
} else {
return true
}
} else {
return true
}
}
}
I need return the result, so I use DispatchSemaphore to block the method. But the question is: This method is called on main queue, and blocking it means blocking the ui thread and the method never returns.
Or any other solution?
I am not sure it's will be work. I does not tested this.
extension CDFFormController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if let entityViewController = self.topViewController as? MyEntityViewController {
if entityViewController.isEditing {
let alert = UIAlertController(title: "Leave the view?", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "leave", style: .default, handler: { _ in
let saveDelegate = navigationBar.delegate;
navigationBar.delegate = nil;
navigationBar.popItem(animated:YES);
navigationBar.delegate = saveDelegate;
}))
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler:nil))
entityViewController.present(alert, animated: true, completion: nil)
return false
} else {
return true
}
} else {
return true
}
}
}
Other way is use RunLoop. But I don't like this.
extension CDFFormController: UINavigationBarDelegate {
public func navigationBar(_ navigationBar: UINavigationBar, shouldPop item: UINavigationItem) -> Bool {
if let entityViewController = self.topViewController as? MyEntityViewController {
if entityViewController.isEditing {
let state = 0
var result = false
let alert = UIAlertController(title: "Leave the view?", message: nil, preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "leave", style: .default, handler: { _ in
state = 1
}))
alert.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: { _ in
state = 2
}))
entityViewController.present(alert, animated: true, completion: nil)
while (state == 0)
{
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.5))
}
return state == 1
} else {
return true
}
} else {
return true
}
}
}
Does anyone knows how to create an UIAlertController like that one that whatsapp did in the next attachment (beside of creating custom UI)
well, as a result from the discussion above, a solution found.
basic idea is to use "contentViewController"
implementation smaple
ViewController.swift
#IBAction func testActionSheetAction(_ sender: Any) {
let sheet = UIAlertController(title: "test", message: nil, preferredStyle: .actionSheet)
let phoneAction = UIAlertAction(title: "", style: .default) { (_) in
print("phone action")
}
phoneAction.mode = .phone
sheet.addAction(phoneAction)
let homeAction = UIAlertAction(title: "", style: .default) { (_) in
print("home action")
}
homeAction.mode = .home
sheet.addAction(homeAction)
sheet.addAction(UIAlertAction(title: "cancel", style: .cancel, handler: nil))
self.present(sheet, animated: true, completion: nil)
}
ActionSheetContentViewController.swift
import UIKit
extension UIAlertAction{
var mode : ActionSheetContentViewController.Mode?{
set{
let vc = ActionSheetContentViewController.viewController(with: newValue)
self.setValue(vc, forKey: "contentViewController")
}
get{
if let vc = value(forKey: "contentViewController") as? ActionSheetContentViewController{
return vc.mode
}
return nil
}
}
}
class ActionSheetContentViewController: UIViewController {
enum Mode{
case home
case phone
var image : UIImage{
get{
switch self {
case .home: return #imageLiteral(resourceName: "icon_home")
case .phone: return #imageLiteral(resourceName: "icon_phone")
}
}
}
var title : String{
get{
switch self {
case .home: return NSLocalizedString("home", comment: "home")
case .phone: return NSLocalizedString("phone", comment: "phone")
}
}
}
}
#IBOutlet weak var label: UILabel!
#IBOutlet weak var imaegView: UIImageView!
var mode : Mode?
override func viewDidLoad() {
super.viewDidLoad()
label.text = mode?.title
imaegView.image = mode?.image
}
class func viewController(with mode : Mode?) -> UIViewController{
let storyboard = UIStoryboard(name: "Main", bundle: .main)
let vc = storyboard.instantiateViewController(withIdentifier: "ActionSheetContentViewController") as! ActionSheetContentViewController
vc.mode = mode
return vc
}
}
ActionSheetContentViewController in storyboard
a screenshot
Yes, you need to add a UITableView to the UIAlertController.
alertController.setValue([customTableGoesHere], forKey: "contentViewController")
I have a project with SpriteKit. I have seen the video of WWDC15 for ReplayKit.
I added ReplayKit to my project and i want record my GameScene and share it in Facebook , save in camera roll more...and more.
anything not working now but i added the functions and i organized all.
thanks!
Start Record
func startScreenRecording() {
// Do nothing if screen recording hasn't been enabled.
let sharedRecorder = RPScreenRecorder.sharedRecorder()
// Register as the recorder's delegate to handle errors.
sharedRecorder.delegate = self
sharedRecorder.startRecordingWithMicrophoneEnabled(true) { error in
if let error = error {
self.showScreenRecordingAlert(error.localizedDescription)
}
}
}
Stop Record
func stopScreenRecordingWithHandler(handler:(() -> Void)) {
let sharedRecorder = RPScreenRecorder.sharedRecorder()
sharedRecorder.stopRecordingWithHandler { (previewViewController: RPPreviewViewController?, error: NSError?) in
if let error = error {
// If an error has occurred, display an alert to the user.
self.showScreenRecordingAlert(error.localizedDescription)
return
}
if let previewViewController = previewViewController {
// Set delegate to handle view controller dismissal.
previewViewController.previewControllerDelegate = self
/*
Keep a reference to the `previewViewController` to
present when the user presses on preview button.
*/
self.presentViewController(previewViewController, animated: true, completion: nil)
}
handler()
}
}
Other Functions of ReplayKit
func showScreenRecordingAlert(message: String) {
// Pause the scene and un-pause after the alert returns.
GameScene().paused = true
// Show an alert notifying the user that there was an issue with starting or stopping the recorder.
let alertController = UIAlertController(title: "ReplayKit Error", message: message, preferredStyle: .Alert)
let alertAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.Default) { _ in
GameScene().paused = false
}
alertController.addAction(alertAction)
/*
`ReplayKit` event handlers may be called on a background queue. Ensure
this alert is presented on the main queue.
*/
dispatch_async(dispatch_get_main_queue()) {
self.view?.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
}
}
func discardRecording() {
// When we no longer need the `previewViewController`, tell `ReplayKit` to discard the recording and nil out our reference
RPScreenRecorder.sharedRecorder().discardRecordingWithHandler {
self.previewViewController = nil
}
}
// MARK: RPScreenRecorderDelegate
func screenRecorder(screenRecorder: RPScreenRecorder, didStopRecordingWithError error: NSError, previewViewController: RPPreviewViewController?) {
// Display the error the user to alert them that the recording failed.
showScreenRecordingAlert(error.localizedDescription)
/// Hold onto a reference of the `previewViewController` if not nil.
if previewViewController != nil {
self.previewViewController = previewViewController
}
}
// MARK: RPPreviewViewControllerDelegate
func previewControllerDidFinish(previewController: RPPreviewViewController) {
previewViewController?.dismissViewControllerAnimated(true, completion: nil)
}
Game Scene
import SpriteKit
import ReplayKit
class GameScene: SKScene , SKPhysicsContactDelegate{
func addRecordButton() {
RecordButton = SKSpriteNode(imageNamed:"Record-Off.png")
RecordButton.name = "RecordButton"
RecordButton.position = CGPoint(x: self.size.width/2 + 185, y: self.size.height/2 + 285)
RecordButton.size = CGSizeMake(32,32)
addChild(RecordButton)
}
func Record() {
let recorder = RPScreenRecorder.sharedRecorder()
if recorder.available{
RecordButton.texture = SKTexture(imageNamed: "Record-On.png")
print("Record")
} else {
RecordButton.texture = SKTexture(imageNamed: "Record-Off.png")
print("Stop Record")
}
}
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
for touch in touches {
let location = touch.locationInNode(self)
if {
if (RecordButton.containsPoint(location)){
Record()
}
}
}
class ViewController: UIViewController, RPScreenRecorderDelegate, RPPreviewViewControllerDelegate {
#IBOutlet weak var startRecordingButton: UIButton!
#IBOutlet weak var stopRecordingButton: UIButton!
#IBOutlet weak var activityView: UIActivityIndicatorView!
private let recorder = RPScreenRecorder.sharedRecorder()
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
if SIMULATOR {
NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: #selector(self.showSimulatorWarning), userInfo: nil, repeats: false)
return
}
}
override func viewDidLoad() {
super.viewDidLoad()
recorder.delegate = self
activityView.hidden = true
buttonEnabledControl(recorder.recording)
}
#IBAction func startRecordingAction(sender: AnyObject) {
activityView.hidden = false
// start recording
recorder.startRecordingWithMicrophoneEnabled(true) { [unowned self] (error) in
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
self.activityView.hidden = true
}
if let error = error {
print("Failed start recording: \(error.localizedDescription)")
return
}
print("Start recording")
self.buttonEnabledControl(true)
}
}
#IBAction func stopRecordingAction(sender: AnyObject) {
activityView.hidden = false
//end recording
recorder.stopRecordingWithHandler({ [unowned self] (previewViewController, error) in
dispatch_async(dispatch_get_main_queue()) {
self.activityView.hidden = true
}
self.buttonEnabledControl(false)
if let error = error {
print("Failed stop recording: \(error.localizedDescription)")
return
}
print("Stop recording")
previewViewController?.previewControllerDelegate = self
dispatch_async(dispatch_get_main_queue()) { [unowned self] in
// show preview vindow
self.presentViewController(previewViewController!, animated: true, completion: nil)
}
})
}
//MARK: - Helper
//control the enabled each button
private func buttonEnabledControl(isRecording: Bool) {
dispatch_async(dispatch_get_main_queue()) {
[unowned self] in
let enabledColor = UIColor(red: 0.0, green: 122.0/255.0, blue: 1.0, alpha: 1.0)
let disabledColor = UIColor.lightGrayColor()
if !self.recorder.available {
self.startRecordingButton.enabled = false
self.startRecordingButton.backgroundColor = disabledColor
self.stopRecordingButton.enabled = false
self.stopRecordingButton.backgroundColor = disabledColor
return
}
self.startRecordingButton.enabled = !isRecording
self.startRecordingButton.backgroundColor = isRecording ? disabledColor : enabledColor
self.stopRecordingButton.enabled = isRecording
self.stopRecordingButton.backgroundColor = isRecording ? enabledColor : disabledColor
}
}
func showSimulatorWarning() {
let actionOK = UIAlertAction(title: "OK", style: .Default, handler: nil)
// let actionCancel = UIAlertAction(title: "cancel", style: .Cancel, handler: nil)
let alert = UIAlertController(title: "ReplayKit不支持模拟器", message: "请使用真机运行这个Demo工程", preferredStyle: .Alert)
alert.addAction(actionOK)
// alert.addAction(actionCancel)
self.presentViewController(alert, animated: true, completion: nil)
}
func showSystemVersionWarning() {
let actionOK = UIAlertAction(title: "OK", style: .Default, handler: nil)
let alert = UIAlertController(title: nil, message: "系统版本需要是iOS9.0及以上才支持ReplayKit", preferredStyle: .Alert)
alert.addAction(actionOK)
self.presentViewController(alert, animated: true, completion: nil)
}
// MARK: - RPScreenRecorderDelegate
// called after stopping the recording
func screenRecorder(screenRecorder: RPScreenRecorder, didStopRecordingWithError error: NSError, previewViewController: RPPreviewViewController?) {
print("Stop Recording ...\n");
}
// called when the recorder availability has changed
func screenRecorderDidChangeAvailability(screenRecorder: RPScreenRecorder) {
let availability = screenRecorder.available
print("Availability: \(availability)\n");
}
// MARK: - RPPreviewViewControllerDelegate
// called when preview is finished
func previewControllerDidFinish(previewController: RPPreviewViewController) {
print("Preview finish");
dispatch_async(dispatch_get_main_queue()) {
[unowned previewController] in
// close preview window
previewController.dismissViewControllerAnimated(true, completion: nil)
}
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}