UIPrintInteractionController's printToPrinter crash in 'lookupLastUsedPrinter' API call.

Hi,

  • We are seeing following crash in our codebase (iOS 15) with following call stack in UIPrintInteractionController.
  • Could anyone please help with your inputs on how we can address following "UIPrintInteractionController's printToPrinter api crash"

Crash stack

uncaught ObjC exception, reason: -[PKPrinter _internalPrinter]: unrecognized selector sent to instance 

Last Exception Backtrace:
2   libobjc.A.dylib                      objc_exception_throw (??:0)w + 
3   CoreFoundation                       __CFOAInitializeNSObject (??:0)3 + 
4   CoreFoundation                       ___forwarding___ (??:0)3 + 
5   CoreFoundation                       __CFRunLoopDoTimers (??:0)0 + 
6   UIKitCore                            __51-[UIPrintPanelViewController lookupLastUsedPrinter]_block_invoke (??:0)5 + 
7   UIKitCore                            -[UIPrintPanelViewController loadView] (??:0)5 + 
8   UIKitCore                            __88-[UIPrintInteractionController _generatePDFInRange:useFullRange:withQuality:completion:]_block_invoke (??:0)5 + 
9   UIKitCore                            -[UIPrintInteractionController printToPrinter:completionHandler:] (??:0)5 + 
10  UIKitCore                            -[UIPrintInteractionController _completePrintPageWithError:] (??:0)5 + 
11  UIKitCore                            -[UIPrintInteractionController printToPrinter:completionHandler:] (??:0)5 + 

Codebase that resulted above crash

UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printControllerT, BOOL completed, NSError *error) {
  [printControllerT setDelegate:nil];                          
};
[printController setDelegate:printDelegate];
[printController setPrintingItem:rurlPrintJob];                    
[printController presentAnimated:YES completionHandler:completionHandler];

More info

  • meanwhile I have found one apple thread where its mentioned that “App Crash when using print of UIPrintInteractionController” ==>https://developer.apple.com/forums/thread/689876.
  • Above thread suggested to use printer.contactPrinter instead of printerController.print.Since our current codebase used default iOS codepath(printToPrinter) with ‘[[UIPrintInteractionController sharedPrintController]presentAnimated:YES completionHandler:completionHandler]]’ api call , I am not able to apply this suggestion in current codebase.

I believe I saw your question on SO, but decided to reply here as well just to share our solution more widely.

We had exact same issue for a while and finally were able to reproduce it and look for a workaround.

Reproducing

The crash is 100% triggered with these steps:

  1. Set up and present UIPrintInteractionController with UIPrintInfo:
let pic = UIPrintInteractionController.shared
pic.printInfo = UIPrintInfo.printInfo()
pic.printingItem = ...
pic.present(animated: true)
  1. Actually print something
  2. In some other code place set up and present UIPrintInteractionController without UIPrintInfo:
let pic = UIPrintInteractionController.shared
pic.printingItem = ...
pic.present(animated: true) // Will crash

 

Solution

Always specify UIPrintInfo for UIPrintIneractionController:

pic.printInfo = UIPrintInfo.printInfo() // Prevents crash
UIPrintInteractionController's printToPrinter crash in 'lookupLastUsedPrinter' API call.
 
 
Q