Absolute Ripple
  • Absolute Ripple
  • Contact

Printing documents, images, etc in iOS

25/3/2012

0 Comments

 
Once you have implemented the ability to open a files in another app using the UIDocumentInteractionController (as described in the previous post), it doesn't take that much effort to enable printing. You essentially only need to implement two further methods in the UIDocumentInteractionController delegate protocol. (The following assume that you have already implemented UIDocumentInteractionController as described in the previous post.)
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.


0 Comments

Your comment will be posted after it is approved.


Leave a Reply.

    Archives

    August 2013
    July 2013
    June 2013
    May 2013
    January 2013
    October 2012
    September 2012
    March 2012
    February 2012

    Categories

    All
    Apple Policy
    In App Purchase
    Ios Programming
    Our Apps

    RSS Feed

    Disclaimers

    Here are some resources that I find useful in writing iOS apps. These are not necessarily unique as they are based on various sources online.
    This is mainly to provide a source of reference to myself and my associates but if it can be of use to someone else, that is good.
    Note that information is provided here as is. Use at your own risk. There is no guarantee that it is accurate. We will update the information as time and resources permit.

    Powered by Create your own unique website with customizable templates.