The additional codes are:
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action
{
if (action == @selector (print:) &&
[UIPrintInteractionController canPrintURL: controller.URL]) {
return YES;
} else {
return NO;
}
}
- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action
{
bool __block success = NO;
if (action == @selector(print:)) {
UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
printController.printingItem = controller.URL;
[printController presentAnimated:YES
completionHandler:^(UIPrintInteractionController *printInteractionController, BOOL completed, NSError *error){
if (completed) {
success = YES;
} else {
NSLog(@"The print job did not complete.");
}
}];
}
return success;
}
The first method tells the UIDocumentInteractionController whether certain actions can be performed in respect of the document. The default response is 'NO'. In this case, we are telling the controller that printing is supported.
The second method is called by the controller when it wants its delegate to perform a particular action, such as printing action here.
Both of these methods make use of a class called UIPrintInteractionController, which handles the printing for you. All you need to do (as done above) is to pass it the document's url.