Tim Mutlow Tim Mutlow - 1 year ago 178
Objective-C Question

Unable to Dismiss the PreviewViewController when Using iOS ReplayKit with Unity 5.3.4 via Custom Plugin

I am attempting to use ReplayKit's native screen recording functionality along with my Unity app so that I can enjoy the benefits of native screen recording and sharing UI.

I have created a Unity plugin which calls my native code written in Objective-C(++) and everything seems to work fine up until it comes to dismissing the


So my record button in the Unity UI triggers the ReplayKit
function and the stop button correctly calls the stopRecording function which returns the
instance to my handler.

In the
, the "Save" button correctly saves to the camera roll and the share button works as expected. But Tapping "Cancel" (or "Done" depending on the context) does nothing.

Here is the code in my
function which deals with the

- (void) stopRecording {

[[RPScreenRecorder sharedRecorder] stopRecordingWithHandler:^(RPPreviewViewController *previewController,NSError *error){
if (previewController != nil) {

previewController.previewControllerDelegate = self;

[previewController willMoveToParentViewController:rootView];

[rootView addChildViewController:previewController];

previewController.view.frame = CGRectMake(0.0, 0.0, rootView.view.bounds.size.width, rootView.view.bounds.size.height);

[rootView.view addSubview:previewController.view];

self.presentedController = previewController;

} else {
NSString *msg = [NSString stringWithFormat:@"The screen recorder is unavailable:\n%@", [error localizedDescription]];
[self displayMessageDialog:msg];

// Implementation of PreviewControllerDelegate protocol
- (void)previewControllerDidFinish:(RPPreviewViewController *)previewController {

[previewController willMoveToParentViewController:nil];

[previewController.view removeFromSuperview];

[previewController removeFromParentViewController];

self.presentedController = nil;


As you can see, I have implemented the protocol for
and attempt to dismiss the view on its is closed event but the delegate function is never being called.

I can't figure out what is wrong, is it something I've coded wrong or is there an intricacy when using with Unity?

I'm stuck so any pointers would be much appreciated!

is used in the above code so here is how I retrieved that reference in the the

static UIViewController *rootView;

- (ReplayKitController*) init {
self = [super init];

if(self && rootView == nil) {
rootView = [[UIApplication sharedApplication] windows][0].rootViewController;

return self;

Answer Source

I finally sorted it after trying a few things but I believe the problem was how I was setting the previewControllerDelegate with

previewController.previewControllerDelegate = self;

And have since changed it to use

[previewController setPreviewControllerDelegate:self];

Which seems to work!

Thanks to all who checked out my question and I hope this helps someone out there!