iPad 上的 AirPrint

AirPrint on the iPad

在我开始之前。是的,我查看了 Apple 文档,并且在这里看到了所有类似的问题。

但出于某种原因,这些答案对我不起作用。

我想从 iPad 打印。此代码适用于 iPhone。当我按下 iPad 上的打印按钮时,屏幕变灰,我可以按出来,但没有任何反应。

这是我的打印代码

  - (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];
    }

}

如果我在 iPhone 上执行此操作,效果会很好。但是由于某种原因 iPad 有问题。我真的不知道还能做什么。

如果我有 10 个代表点数,我就能 post 我提到的灰色屏幕,但我不能那样做,抱歉: (

感谢@Paulw11,他告诉我需要出示 fromRect 我的打印按钮。

所以我所做的就是为我的按钮创建一个出口并从中呈现并且它起作用了!

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