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?

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

  • The same problem.

Add a Comment

Replies

same problem

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

  • An update from Apple, the status of my ticket is now: "Resolution: Potential fix identified - For a future OS update". So hopefully should see something in a beta build soon

Add a Comment

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.

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.

Post not yet marked as solved Up vote reply of AS_H Down vote reply of AS_H

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

  • Here is the current status of my ticket referenced above: "Recent Similar Reports:Less than 10 Resolution:Open"

    Apple does not appear to make these tickets public, so you should file your own ticket in feedback assistant using "Printing" as the category, describing your crash, and collectively our similar tickets will increase the scope of this crash and hopefully get addressed by Apple after it gets deemed a priority.

Add a Comment

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

  • Problem exists with iPadOS 15.2. (19C5026i)

Add a Comment

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

  • Thank you, I've tried it and it seems to work fine for me too.

  • Working for me as well, thanks!

  • Thanks for the workaround, this is working for me as well. Anyone using this code, keep in mind the following in the documentation when using the contactPrinter function: "Calling this method can take a significantly long time (up to 30 seconds)". Once Apple provides a real fix to this bug, we shouldn't have to depend on this, but this fixes it for our users in the meantime!

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.

  • I am using this code and it works:

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController]; UIPrinter *printer = [self savedPrinter]; // get the printer you want ... [printer contactPrinter:^(BOOL available) {                 if (available) {                     [pic printToPrinter:printer completionHandler:completionHandler];                 } else {                     NSLog(@"printer unavailable");                 }             }];
  • The way in the comment above does not work for me = ( And i tried like this, and too not working: [self.printer contactPrinter:^(BOOL available) {     if (available) {       [pic1 printToPrinter: self.printer completionHandler:^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {               }       }];     }   }];

  • @manjitbhullar1 You can try this, but it pretty much looks like the examples already provided, perhaps you need to dispatch your code to a different thread? https://gist.github.com/rwyland/3dd3f8727effb92d554d773e11208286

Add a Comment

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