The answer here really lies in Chaithra's comment:
UIDocumentInteractionController: invalid scheme http. Only the file scheme is supported
To solve this problem you need to download the PDF file, obtain a local URL for it and can then pass it onto the UIDocumentInteractionController
. Using AFNetworking as a baseline the following worked for me:
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSProgress *progress = nil;
NSURLSessionDownloadTask *downloadTask = [manager downloadTaskWithRequest:request progress:&progress destination:^NSURL *(NSURL *targetPath, NSURLResponse *response) {
NSURL *documentsDirectory = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory
inDomain:NSUserDomainMask
appropriateForURL:nil
create:NO error:nil];
return [documentsDirectory URLByAppendingPathComponent:[response suggestedFilename]];
} completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {
[self displayDocument:filePath];
}];
[downloadTask resume];
My displayDocument:
method looks like this:
- (void)displayDocument:(NSURL*)document {
UIDocumentInteractionController *documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:document];
documentInteractionController.delegate = self;
[documentInteractionController presentPreviewAnimated:YES];
}
And the following delegate method:
- (UIViewController*)documentInteractionControllerViewControllerForPreview:(UIDocumentInteractionController *)controller {
return self;
}
Without using AFNetworking
the code would be broadly similar as most of the classes used in the above sample are standard anyway. I happen to be using AFNetworking in this instance to simplify some of the setup and automatically using the categories it provides around UIProgress
to update a progress bar for me.