Search code examples
iosipadairprint

AirPrint on the iPad


Before I get started. Yes I have looked in Apple documentation and I have saw all the questions similarly asked on here.

But for some reason, those answers don't work with me.

I am looking to print from an iPad. This code works with an iPhone. When I press the print button I have, on the iPad, the screen gets a grayed over look that I can press out of but nothing happens.

Here is my code for printing

  - (IBAction)printButton:(id)sender {
        NSMutableString *printFields = [NSMutableString     stringWithFormat:@"%@", _nameField.text];

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


    UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    printInfo.outputType = UIPrintInfoOutputGeneral;
    printInfo.jobName = @"Printing sign up sheet.";
    pic.printInfo = printInfo;

    UISimpleTextPrintFormatter *textFormatter = [[UISimpleTextPrintFormatter alloc]initWithText:printFields];
    textFormatter.startPage = 0;
    textFormatter.contentInsets = UIEdgeInsetsMake(72, 72, 72, 72);
    textFormatter.maximumContentWidth = 6 * 72;
    pic.printFormatter = textFormatter;
    pic.showsPageRange = YES;


    void(^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
        if (!completed && error) {
            NSLog(@"Print error: %@", error);
        }
    };

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
        [pic presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];
    }
    else {
        [pic presentAnimated:YES completionHandler:completionHandler];
    }

}

If I do this on an iPhone, it works just fine. But for some reason the iPad has trouble. I really don't know what else to do.

If I had 10 rep points, I would be able to post the grey screen I mentioned, but I can't do that, sorry : (


Solution

  • Thank you to @Paulw11 who showed me that I needed to present fromRect of my print button.

    So what I did was create an outlet for my button and present from that and it worked!

    if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
            [pic presentFromRect:self.printOutlet.frame inView:self.view animated:YES completionHandler:completionHandler];
        }
        else {
            [pic presentAnimated:YES completionHandler:completionHandler];
        }