iOS UIPrintInteractionController printToPrinter Contacting printer

416

The issue is not related to the iOS printing functionality but comes from the plugin implementation of the direct print.
See here for more info: https://github.com/DavBfr/dart_pdf/issues/37

It's the current usage of callback and thread that makes the UI unresponsive.

Share:
416
Tristan Pct
Author by

Tristan Pct

Updated on December 09, 2022

Comments

  • Tristan Pct
    Tristan Pct 11 months

    I'm currently developing an app using direct print through UIPrintInteractionController printToPrinter method but when I launch a print order a popup comes up saying:

    Printing to "my-printer.local."
    Contacting printer...

    But I don't have any print order in my printer, and the app is stuck with this popup (tap the "Cancel" button do nothing).

    Here is my method to print:

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

    The printerURL comes from UIPrinterPickerController presentAnimated method.

    status variable is true.

    I tried with an iPhone Simulator + Printer Simulator, here is the printer logs for one method call:

    [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.
    

    NOTE 1: printing using UIPrintInteractionController presentAnimated works perfectly (using simulator or real device).

    NOTE 2: the following code is used inside a Flutter plugin.