iOS UIPrintInteractionController printToPrinter联系打印机

时间:2019-02-21 14:59:14

标签: ios objective-c printing flutter

我目前正在通过UIPrintInteractionController printToPrinter方法使用直接打印来开发应用程序,但是当我启动打印命令时,会弹出一个对话框:

  

打印到“ my-printer.local”。
  正在联系打印机...

但是我的打印机没有任何打印命令,并且该应用程序卡在该弹出窗口中(点击“取消”按钮不执行任何操作)。

这是我的打印方法:

- (void)directPrint:(nonnull NSString*)name withPrinter:(NSURL*)printerURL {
  UIPrintInteractionController* controller = [UIPrintInteractionController sharedPrintController];
  [controller setDelegate:self];

  UIPrintInfo* printInfo = [UIPrintInfo printInfo];
  printInfo.jobName = name;
  printInfo.outputType = UIPrintInfoOutputGeneral;
  controller.printInfo = printInfo;
  renderer = [[PdfPrintPageRenderer alloc] init:channel];
  [controller setPrintPageRenderer:renderer];

  UIPrintInteractionCompletionHandler completionHandler =
      ^(UIPrintInteractionController* printController, BOOL completed, NSError* error) {
        NSLog(@"Handler completed: %d", completed);
        if (!completed && error) {
          NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, (unsigned int)error.code);
        }
        self->renderer = nil;
      };

  UIPrinter* printer = [UIPrinter printerWithURL:printerURL];

  BOOL status = [controller printToPrinter:printer completionHandler:completionHandler];

  NSLog(@"Print status: %d", status);
}

printerURL来自UIPrinterPickerController presentAnimated方法。

status变量为true

我尝试使用iPhone模拟器+打印机模拟器,这是一个方法调用的打印机日志:

[21/Feb/2019:14:40:49 +0100] [Client 46] Accepted connection from 192.168.1.131:57092 (IPv4)
[21/Feb/2019:14:40:49 +0100] [Client 46] Encrypting connection.
[21/Feb/2019:14:40:49 +0100] [Client 46] Connection from 192.168.1.131 now encrypted.
[21/Feb/2019:14:40:49 +0100] [Client 47] Accepted connection from 192.168.1.131:57093 (IPv4)
[21/Feb/2019:14:40:49 +0100] [Client 47] Encrypting connection.
[21/Feb/2019:14:40:49 +0100] [Client 47] Connection from 192.168.1.131 now encrypted.

注释1:使用UIPrintInteractionController presentAnimated进行打印非常有效(使用模拟器或真实设备)。

注意2::Flutter插件内部使用了以下代码。

1 个答案:

答案 0 :(得分:0)

此问题与iOS打印功能无关,而是来自直接打印的插件实现。
有关更多信息,请参见此处:https://github.com/DavBfr/dart_pdf/issues/37

正是回调和线程的当前用法使UI无法响应。