Skip to content

Instantly share code, notes, and snippets.

@stollcri
Last active May 9, 2018 18:11
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save stollcri/732def91d09525deb49c to your computer and use it in GitHub Desktop.
Save stollcri/732def91d09525deb49c to your computer and use it in GitHub Desktop.
Created a "Single View Application" project and added ReplayKit code to the ViewController files
//
// ViewController.h
// ReplayKitExample
//
// Created by Christopher Stoll on 6/11/15.
// Copyright © 2015 Christopher Stoll. All rights reserved.
//
#import <UIKit/UIKit.h>
#import <ReplayKit/ReplayKit.h>
@interface ViewController : UIViewController <RPScreenRecorderDelegate, RPPreviewViewControllerDelegate>
@property (weak, nonatomic) RPPreviewViewController *previewViewController;
- (IBAction)doRecord:(id)sender;
- (IBAction)doPreviewSave:(id)sender;
@end
//
// ViewController.m
// ReplayKitExample
//
// Created by Christopher Stoll on 6/11/15.
// Copyright © 2015 Christopher Stoll. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - ReplayKit
- (void)startScreenRecording {
RPScreenRecorder *sharedRecorder = RPScreenRecorder.sharedRecorder;
sharedRecorder.delegate = self;
[sharedRecorder startRecordingWithMicrophoneEnabled:YES handler:^(NSError *error) {
if (error) {
NSLog(@"startScreenRecording: %@", error.localizedDescription);
}
}];
}
- (void)stopScreenRecording {
RPScreenRecorder *sharedRecorder = RPScreenRecorder.sharedRecorder;
[sharedRecorder stopRecordingWithHandler:^(RPPreviewViewController *previewViewController, NSError *error) {
if (error) {
NSLog(@"stopScreenRecording: %@", error.localizedDescription);
}
if (previewViewController) {
previewViewController.previewControllerDelegate = self;
self.previewViewController = previewViewController;
// RPPreviewViewController only supports full screen modal presentation.
self.previewViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[self.view.window.rootViewController presentViewController:previewViewController animated:YES completion:nil];
}
}];
}
#pragma mark - RPScreenRecorderDelegate
- (void)screenRecorder:(RPScreenRecorder *)screenRecorder didStopRecordingWithError:(NSError *)error previewViewController:(nullable RPPreviewViewController *)previewViewController {
// handle error which caused unexpected stop of recording
}
- (void)screenRecorderDidChangeAvailability:(RPScreenRecorder *)screenRecorder {
// handle screen recorder availability changes
}
#pragma mark - RPPreviewViewControllerDelegate
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController {
[previewController dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - Interface Actions
- (IBAction)doRecord:(id)sender {
[self startScreenRecording];
}
- (IBAction)doPreviewSave:(id)sender {
[self stopScreenRecording];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment