How do you implement sharing with ReplayKit? - ios

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()
}
}

Related

Pick Video And Pics from photo gallary please

I'm desperately trying to add video picking option to my app. The app can take a picture and select pictures but it cannot select video file. I want the users to be able to select video from the photo library just as easily as they can select a photo. But most people I have asked seem to think it is not possible. But of course, it is not. I have used many app with this feature. Or this is not something I can do with Xcode?
}
let actionsheet = UIAlertController(title: nil, message: nil, preferredStyle: .alert)
actionsheet.addAction(UIAlertAction(title: "Gallery", style: .default, handler: { (alert: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary){
let mypickerController = UIImagePickerController()
mypickerController.delegate = self
mypickerController.sourceType = .photoLibrary
self.present(mypickerController, animated: true, completion: nil)
}
}))
actionsheet.addAction(UIAlertAction(title: "Camera", style: .default, handler: { (alert: UIAlertAction) in
if UIImagePickerController.isSourceTypeAvailable(.camera){
let myPickerController = UIImagePickerController()
myPickerController.delegate = self
myPickerController.sourceType = .camera
self.present(myPickerController, animated: true, completion: nil)
}
}))
actionsheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
self.present(actionsheet, animated: true, completion: nil)
}
#IBAction func onSaveButton(_ sender: Any) {
print("screenshot pressed")
//let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
//appDelegate?.myInterstitial = appDelegate?.createAndLoadInterstitial()
if firstImage.image == nil{
GlobalFunction.sharedManager.setWhiteBackground(selectView: firstImage)
}
if secondImage.image == nil{
GlobalFunction.sharedManager.setWhiteBackground(selectView: secondImage)
}
if thirdImage.image == nil{
GlobalFunction.sharedManager.setWhiteBackground(selectView: thirdImage)
}
let image = GlobalFunction.sharedManager.takeScreenshot(theView: backView)//takeScreenshot(theView: backView)
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
}
#IBAction func onCancelButton(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(vc, animated: true, completion: nil)
}
#IBAction func onAddButton(_ sender: Any) {
let vc = self.storyboard?.instantiateViewController(withIdentifier: "mainVC") as! ViewController
self.present(vc, animated: true, completion: nil)
}
#objc func image(image: UIImage, didFinishSavingWithError error: NSErrorPointer, contextInfo: UnsafeRawPointer) {
DispatchQueue.main.async{
self.showAlertView(message: "Your story has been saved to your Camera Roll", completionHandler: { (a) in
let appDelegate: AppDelegate? = UIApplication.shared.delegate as? AppDelegate
appDelegate?.myInterstitial = appDelegate?.createAndLoadInterstitial()
})
//UIAlertView(title: "Success", message: "Your story has been saved to your Camera Roll", delegate: nil, cancelButtonTitle: "Close").show()
}
}
func takeScreenshot(theView: UIView) -> UIImage {
UIGraphicsBeginImageContextWithOptions(theView.bounds.size, true, 0.0)
theView.drawHierarchy(in: theView.bounds, afterScreenUpdates: true)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return image!
}
func circleCropDidCancel() {
dismiss(animated: false, completion: nil)
}
func circleCropDidCropImage(_ image: UIImage) {
dismiss(animated: false, completion: nil)
if selected == 1{
firstImage.image = image
}else if selected == 2{
secondImage.image = image
}else if selected == 3{
thirdImage.image = image
}
}
}
UIImagePickerController has a mediaTypes property. It determines what kind of media the picker can pick. You are not setting mypickerController.mediaTypes to include videos.

how to save and fetch button enable and disable value using Userdefaults

I want to save the button enable and disable value in userdefault. I wrote the following code but nothing happened. Please tell me how to save and fetch button enable and disable value.
I want, when the ibaction button is pressed, it should disable and when closes the app and reopen the app then it should save the disable state of button.
import UIKit
class ViewController: UIViewController {
#IBOutlet weak var btn2: UIButton!
#IBOutlet weak var btn1: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
// let def = UserDefaults.standard.bool(forKey: "val")
// btn1.isEnabled = def
}
override func viewWillAppear(_ animated: Bool) {
let def = UserDefaults.standard.bool(forKey: "val")
print(def)
//btn1.isEnabled = def
}
#IBAction func btn1Pressed(_ sender: UIButton) {
let def = UserDefaults.standard.bool(forKey: "val")
print("Button one pressed")
let otherAlert = UIAlertController(title: "Button 1!!", message: "Here is button one.", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (UIAlertAction) in
print("OK pressed")
self.btn1.isEnabled = false
UserDefaults.standard.set(self.btn1.isEnabled, forKey: "val")
}
let dismiss = UIAlertAction(title: "No", style: UIAlertActionStyle.cancel) { (UIAlertAction) in
self.btn1.isEnabled = true
UserDefaults.standard.set(self.btn1.isEnabled, forKey: "val")
print("No pressed")
}
otherAlert.addAction(dismiss)
otherAlert.addAction(okaction)
present(otherAlert, animated: true, completion: nil)
let def1 = UserDefaults.standard.bool(forKey: "val")
btn1.isEnabled = def1
}
#IBAction func btn2Pressed(_ sender: UIButton) {
print("Button Two pressed")
}
}
Instead of saving self.btn1.isEnabled save the value you are applying to that. Can't you do this directly ?
override func viewDidLoad() {
super.viewDidLoad()
// You need to check if key is present or else you will get false as default value and your button will disabled only
if isKeyPresentInUserDefaults(key: "val") {
let def = UserDefaults.standard.bool(forKey: "val")
btn1.isEnabled = def
}
}
// Check key is present or not
func isKeyPresentInUserDefaults(key: String) -> Bool {
return UserDefaults.standard.object(forKey: key) != nil
}
#IBAction func btn1Pressed(_ sender: UIButton) {
let defaults = UserDefaults.standard
defaults.set(false, forKey: "val")
print("Button one pressed")
let otherAlert = UIAlertController(title: "Button 1!!", message: "Here is button one.", preferredStyle: UIAlertControllerStyle.alert)
let okaction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) { (UIAlertAction) in
print("OK pressed")
self.btn1.isEnabled = false
defaults.set(false, forKey: "val")
}
let dismiss = UIAlertAction(title: "No", style: UIAlertActionStyle.cancel) { (UIAlertAction) in
self.btn1.isEnabled = true
defaults.set(true, forKey: "val")
print("No pressed")
}
otherAlert.addAction(dismiss)
otherAlert.addAction(okaction)
present(otherAlert, animated: true, completion: nil)
// No need of these 2 lines
let def1 = defaults.bool(forKey: "val")
btn1.isEnabled = def1
}

BaseViewController subclasses are not releasing in swift 3

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
}

UIAlertController action sheet whatsapp style

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")

How to record game scene in spritekit

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.
}
}

Resources