14

How can make a IBAction method for printing a UITextView with AirPrint in objective-c?

Black Frog
  • 11,595
  • 1
  • 35
  • 66
Rafael
  • 403
  • 2
  • 6
  • 9

2 Answers2

24

Check whether printing is available:

if ([UIPrintInteractionController isPrintingAvailable])
{
    // Available
} else {
    // Not Available
}

Print after button click:

-(IBAction) buttonClicked: (id) sender;
{
    NSMutableString *printBody = [NSMutableString stringWithFormat:@"%@, %@",self.encoded.text, self.decoded.text];
    [printBody appendFormat:@"\n\n\n\nPrinted From *myapp*"];

     UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
     pic.delegate = self;

     UIPrintInfo *printInfo = [UIPrintInfo printInfo];
     printInfo.outputType = UIPrintInfoOutputGeneral;
     printInfo.jobName = self.titleLabel.text;
     pic.printInfo = printInfo;

     UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc] initWithText:printBody];
     textFormatter.startPage = 0;
     textFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
     textFormatter.maximumContentWidth = 6 * 72.0;
     pic.printFormatter = textFormatter;
     [textFormatter release];
     pic.showsPageRange = YES;

     void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
     ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
     if (!completed && error) {
     NSLog(@"Printing could not complete because of error: %@", error);
     }
     };

    [pic presentFromBarButtonItem:self.rightButton animated:YES completionHandler:completionHandler];

}

Originally posted by '87vert' at iPhone Dev SDK: Airprint Tutorial - Simple Print File

prairiedogg
  • 6,323
  • 8
  • 44
  • 52
Anne
  • 26,765
  • 9
  • 65
  • 71
  • It works fine, make sure you implemented it correctly. Check the post by 'twerner' at iPhone Dev SDK for another example (link to specific thread provided in answer). – Anne Apr 17 '11 at 02:13
  • That was great. I wasn't planning on implementing AirPrinting in my app but I just tried it from this tutorial and I finished in like 10 minutes. Thats awesome. Thanks. – Andrew Apr 17 '11 at 15:41
  • what the hell is "encoded.text" and "decoded.text" ?! – Fattie Jun 29 '14 at 21:08
  • This is silly - it just prints the unformatted text string, from the UITextView. At least, you can print it as a rendered bitmap (more easily) .. http://stackoverflow.com/questions/24479985 – Fattie Jun 29 '14 at 21:29
  • If needed, you will need to add UIPrintInteractionControllerDelegate to the header to receive messages from the dialog. – Nick N Aug 29 '14 at 17:20
2

The following method uses the name of the file to be printed and also the bar button code from where you want the airprint popup to be shown. It works for me and im sure will be helpfull

-(void)printJob:(int)jobType:(NSString*)jobName:(UIBarButtonItem *)barButton{

NSString *path;
if ([jobName isEqualToString:@"Printout.png"]) {
    path= [self documentsPathForFileName:@"Printout.png"];
}

NSData *mydata=[NSData dataWithContentsOfFile:path];
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.delegate = self;
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = mydata;
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    if (!completed && error) {
        NSLog(@"Printing could not complete because of error: %@", error);
    }
};

[pic presentFromBarButtonItem:barButton animated:YES completionHandler:completionHandler];

}
JSA986
  • 5,870
  • 9
  • 45
  • 91
Shantanu
  • 3,086
  • 3
  • 25
  • 32