App Crash when using print of UIPrintInteractionController

Using the latest Xcode 13 Beta 5 and try to print a PDF document on a predefined printer the App Crashes.

I created a new iOS App running it on Simulator iPhone 12/iOS 15 with the following code:

let a4Document = Bundle.main.url(forResource: "A4", withExtension: "pdf")!
let airPrinter = UIPrinter(url: URL(string: "ipps://oki-c332.local.:443/ipp/print")!)

struct ContentView: View {
    var body: some View {
        Button("Print") {
            DispatchQueue.main.async {
                let printController = UIPrintInteractionController.shared
                let printInfo = UIPrintInfo(dictionary:nil)
                printInfo.jobName = "AirPrint"
                printInfo.outputType = UIPrintInfo.OutputType.grayscale
                printController.printInfo = printInfo
                printController.printingItem = a4Document
                printController.print(to: airPrinter)
            }
        }
    }
}

It failed when trying to print with:

2021-09-13 10:59:01.880869+0200 AirPrintTest[40566:741434] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Application tried to present modally a view controller <UIAlertController: 0x7fb79a035c00> that is already being presented by <UIViewController: 0x7fb7987229c0>.'
2*** First throw call stack:
3(
4	0   CoreFoundation                      0x00007fff203fc8a8 __exceptionPreprocess + 242
5	1   libobjc.A.dylib                     0x00007fff2019ebe7 objc_exception_throw + 48
6	2   UIKitCore                           0x00007fff2484b382 -[UIViewController _presentViewController:withAnimationController:completion:] + 5854
7	3   UIKitCore                           0x00007fff2484bf6c __63-[UIViewController _presentViewController:animated:completion:]_block_invoke_2 + 70
8	4   UIKitCore                           0x00007fff2559baa5 +[UIView(Animation) performWithoutAnimation:] + 84
9	5   UIKitCore                           0x00007fff2484bec1 __63-[UIViewController _presentViewController:animated:completion:]_block_invoke + 211
10	6   UIKitCore                           0x00007fff2484c186 -[UIViewController _performCoordinatedPresentOrDismiss:animated:] + 519
11	7   UIKitCore                           0x00007fff2484bdae -[UIViewController _presentViewController:animated:completion:] + 173
12	8   UIKitCore                           0x00007fff2484c23c -[UIViewController presentViewController:animated:completion:] + 155
13	9   UIKitCore                           0x00007fff24faee9e -[UIPrintingProgress _mainQueue_presentProgressAlert] + 395
14	10  UIKitCore                           0x00007fff24faf2fe -[UIPrintingProgress _mainQueue_showProgress:immediately:] + 844
15	11  UIKitCore                           0x00007fff24faef03 -[UIPrintingProgress showProgress:immediately:] + 66
16	12  UIKitCore                           0x00007fff24faf8ae -[UIPrintingProgress setPrintInfoState:] + 119
17	13  UIKitCore                           0x00007fff24fbf50c -[UIPrintInteractionController _setPrintInfoState:] + 80
18	14  UIKitCore                           0x00007fff24fb8d36 __65-[UIPrintInteractionController printToPrinter:completionHandler:]_block_invoke.151 + 236
19	15  libdispatch.dylib                   0x0000000100ef3a18 _dispatch_call_block_and_release + 12
20	16  libdispatch.dylib                   0x0000000100ef4bfc _dispatch_client_callout + 8
21	17  libdispatch.dylib                   0x0000000100f03366 _dispatch_main_queue_callback_4CF + 1195
22	18  CoreFoundation                      0x00007fff2036a555 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
23	19  CoreFoundation                      0x00007fff20364db2 __CFRunLoopRun + 2772
24	20  CoreFoundation                      0x00007fff20363dfb CFRunLoopRunSpecific + 567
25	21  GraphicsServices                    0x00007fff2cbb5cd3 GSEventRunModal + 139
26	22  UIKitCore                           0x00007fff24fee193 -[UIApplication _run] + 928
27	23  UIKitCore                           0x00007fff24ff2bfb UIApplicationMain + 101
28	24  SwiftUI                             0x00007fff5cc8f6d5 $s7SwiftUI17KitRendererCommon33_ACC2C5639A7D76F611E170E831FCA491LLys5NeverOyXlXpFAESpySpys4Int8VGSgGXEfU_ + 196
29	25  SwiftUI                             0x00007fff5cc8f60f $s7SwiftUI6runAppys5NeverOxAA0D0RzlF + 148
30	26  SwiftUI                             0x00007fff5c685e55 $s7SwiftUI3AppPAAE4mainyyFZ + 61
31	27  AirPrintTest                        0x0000000100dcf99e $s12AirPrintTest0abC3AppV5$mainyyFZ + 30
32	28  AirPrintTest                        0x0000000100dcfa19 main + 9
33	29  dyld                                0x0000000100fe4e1e start_sim + 10
34	30  ???                                 0x00000001062794d5 0x0 + 4398224597
35)
36libc++abi: terminating with uncaught exception of type NSException

Is this bug known?

same problem

That's SwiftUI, not UIKit ? Did you try without dispatching ?

same problem as well, but in obj-c. Filed in feedbackassistant with id FB9431113 on July 30th

Same problem, in iOS 15

Same issue. Works with iOS 14.8 but same exception once using iOS 15. Using Xcode 13.

Same issue.

I have contacted Apple Developer Technical Support and they said

this is a known problema dn there is no workaround availble at this time. I recommend you continue testing your app to see if a fix is provided in system updates.

I think now it's better if everyone feel a bug via Feedback Assistant to make our voices louder. If you can attach sample code and app console output - that will be even better. Maybe when Apple will fix this issue faster.

The same problem.

Not a great work around, but using the

func present(animated: Bool, completionHandler completion: UIPrintInteractionController.CompletionHandler? = nil) -> Bool

instead of

func print(to printer: UIPrinter, completionHandler completion: UIPrintInteractionController.CompletionHandler? = nil) -> Bool

works and you can print and it doesn't crash, however, the user has to interact and select the printer and other settings, so it's not as efficient.

Do we know whether this issue has been resolved? Is there any bug tracker that apple published to see the status of the item? We have an enterprise app which is crashing on this method and we can only lock the ios upgrade for 90 days, so we hope this issue can be resolved before the lock is expired. Any help will be appreciated

Problem still present with iOS 15.1, ticket opened on Feedback Assistant: FB9713300 (App crash with method -(BOOL)printToPrinter:(UIPrinter*)printer completionHandler:(UIPrintInteractionCompletionHandler)completion)

Seeing this too on iPad OS 15.1

I think I may have a solution.

Instead of

printController.print(to: printer, completionHandler: printCompletionHandler)

try

printer.contactPrinter { (available) -> Void in
    if (available) {
        printController.print(to: printer, completionHandler: printCompletionHandler)
    }
}

This seemed to fix the issue for me.

Seems to be one of those times where Apple change things just enough to break them without telling anyone how...

hi @rwyland7. can you please share you code snippet how you are using this . I am using the same in Objective - C but getting the crash.

This error has been solved with the new version of ios 15.2.1

No it's not. The app still crashing on iOS 16. In simulators. On iPhone Xs - with iOS 16. When on Printer Options - I'm picking Printer. In PKPrinterBrowser - there is NSInternalInconsistencyException related to UITableView updates. "attempt to delete row 2 from section 0 which only contains 1 rows before the update".

Apple Team! You need to resolve that asap.

App Crash when using print of UIPrintInteractionController
 
 
Q