I solved this by wrapping the UIDocumentInteractionController
in a UINavigationController
and switching the application window's root view controller to the navigation controller for presentation. In my usage the other view controllers were not using UINavigationController
so upon dismissal we swap the old root controller back:
#import "MainViewController.h"
@interface MainViewController ()
@property (nonatomic, strong) UINavigationController *navController;
@property (nonatomic, strong) MainViewController *main;
@end
@implementation MainViewController
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
self.main = self;
self.navController = [[UINavigationController alloc] initWithRootViewController:[UIViewController new]];
[[UIApplication sharedApplication].keyWindow setRootViewController:self.navController];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"example" ofType:@"pdf"];
NSURL *url = [NSURL fileURLWithPath:filePath];
UIDocumentInteractionController *pdfViewer = [UIDocumentInteractionController interactionControllerWithURL:url];
[pdfViewer setDelegate:self];
[pdfViewer presentPreviewAnimated:YES];
}
- (UIViewController *)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller
{
return self.navController;
}
- (void)documentInteractionControllerDidEndPreview:(UIDocumentInteractionController *)controller
{
[[UIApplication sharedApplication].keyWindow setRootViewController:self.main];
self.main = nil;
}
- (void)dismiss
{
[self.navController popViewControllerAnimated:YES];
}
@end
The dummy view controller allows the interaction controller to be popped (back button).