Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UIDocumentInteractionController not showing print option

I have code to show a document as follows:

documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:self.thisUrl];

NSString *pathExtension = [self.thisUrl pathExtension];
if (pathExtension) {
    NSString *UTI = (__bridge NSString*)UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (__bridge CFStringRef)(pathExtension), NULL);
    if (UTI) {
        documentInteractionController.UTI = UTI;
    }
}
documentInteractionController.delegate = self;
[documentInteractionController presentOptionsMenuFromBarButtonItem:shareButton animated:YES];

When the options menu is displayed, it shows a list of apps that can open the document (e.g. Message), along with a list of actions below.

The options menu shows a list actions that is different from the menu shown in e.g., the Mail app.

The main difference is that the Mail app shows a "print" option, while my options menu does not. How do I get the options menu to show the print option?

The options menu shown in my app

The options menu shown in Mail

EDIT: I did a further test where I implemented the methods:

- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller canPerformAction:(SEL)action
{
  return YES;
}

- (BOOL)documentInteractionController:(UIDocumentInteractionController *)controller performAction:(SEL)action
{
  return YES; // or NO, doesn't matter
}

This had the effect of showing the "print", "copy" and "save to camera roll" actions in the popup view. Nothing happened when I tapped them, probably because I didn't properly implement -performAction. I also get a warning in the console log about using legacy methods.

This was a step backwards in some ways because I could no longer print some documents which were able to print correctly with the document interaction controller before I added those methods.

like image 912
1800 INFORMATION Avatar asked Jun 02 '15 05:06

1800 INFORMATION


1 Answers

Apple encourage you to use UIActivityViewController. You can easily achieve this with that. However Print option is available only if your sharing content type supports printing. You can see a list of supported activities by data types here

- (IBAction)shareButton:(UIBarButtonItem *)sender
{
    NSString *textToShare = @"Text to share";
    NSURL *myWebContent = [NSURL URLWithString:@"http://yourpath.com/yourfile.pdf"]; // set your printable file here!

NSData *myData = [NSData dataWithContentsOfURL:myWebContent];


    NSArray *objectsToShare = @[textToShare, myData];

    UIActivityViewController *activityVC = [[UIActivityViewController alloc] initWithActivityItems:objectsToShare applicationActivities:nil];

//Add exclusions here
    NSArray *excludeActivities = @[UIActivityTypeAirDrop,
                                   UIActivityTypeAssignToContact,
                                   UIActivityTypeSaveToCameraRoll,
                                   UIActivityTypeAddToReadingList,
                                   UIActivityTypePostToFlickr,
                                   UIActivityTypePostToVimeo];

    activityVC.excludedActivityTypes = excludeActivities;

    [self presentViewController:activityVC animated:YES completion:nil];
}
like image 200
Rukshan Avatar answered Oct 03 '22 06:10

Rukshan