使用UISimpleTextPrintFormatter进行打印时出现UIPrintErrorDomain错误4

时间:2015-07-08 10:27:59

标签: ios airprint

我有以下代码来打印简单文本。我使用的是HP Officejet 4630系列。但我收到UIPrintErrorDomain错误4以及错误3。

知道为什么吗?

UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
pi.jobName = @"test";

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;

UISimpleTextPrintFormatter *formatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"testing 123"];
formatter.contentInsets = UIEdgeInsetsMake(72, 72, 72, 72);
pic.printFormatter = formatter;

NSString *url = [[NSUserDefaults standardUserDefaults] objectForKey:@"printer"];
UIPrinter *printer = [UIPrinter printerWithURL:[NSURL URLWithString:url]];

[pic printToPrinter:printer completionHandler:^(UIPrintInteractionController * __nonnull printInteractionController, BOOL completed, NSError * __nullable error) {
    NSLog(@"error -> %@", error);
}];

我有正确的打印机网址。 我收到了以下错误。

ipp://HPD0...15287.local.:631/ipp/print: startJob: Unable to connect to printd: Bad file descriptor

Error Domain=UIPrintErrorDomain Code=4 "The operation couldn’t be completed. (UIPrintErrorDomain error 4.)"

1 个答案:

答案 0 :(得分:0)

也浪费了一些时间寻求解决方案。 发现了这样一个:

  1. 首先-首次选择打印机(与ios 13之前一样)并保存其打印机NSURL。
  private selectPrinter() {
    const printPicker = UIPrinterPickerController.printerPickerControllerWithInitiallySelectedPrinter(null);
    const view = utils.ios
      .getter(UIApplication, UIApplication.sharedApplication).keyWindow.rootViewController.view;
    const theFrame = frame.topmost().currentPage.frame;

    printPicker.presentFromRectInViewAnimatedCompletionHandler(
      theFrame, view, true, (printerPicker, userDidSelect, error) => {
        if (userDidSelect) {
          this.someService.printerUrl = printerPicker.selectedPrinter.URL;
        } else {
          setTimeout(() => { this.selectPrinter(); }, 4);
        }
      },
    );
  }
  1. 然后使用NSURL来使用打印机,而不必每次都选择打印机 特别感谢@moeseth的想法。
    const printer = UIPrinter.printerWithURL(this.someService.printerUrl);

    printer.contactPrinter((available) => {
      if (available) {
        controller.printToPrinterCompletionHandler(printer, callback);
      } else {
        alert("Printer Not Found");
      }
    });

希望它将对某人有所帮助