UIPrintInteractionController crashed on "Cancel" button clicked on iOS 14 only

421 Views Asked by At

My code is :

UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;

NSString *url=[[req URL] absoluteString];
pi.jobName = url;

pi.orientation = UIPrintInfoOrientationPortrait;
pi.duplex = UIPrintInfoDuplexLongEdge;

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
pic.delegate=self;
pic.showsPageRange = YES;
pic.printFormatter = self.webView.viewPrintFormatter;


dispatch_async(dispatch_get_main_queue(), ^(void)
{
   [pic presentFromRect:CGRectMake(64, 64, self.view.frame.size.width,  self.view.frame.size.height-64) inView:self.view animated:YES completionHandler:^(UIPrintInteractionController* pic2, BOOL completed, NSError* error) {
    
}];
    
});

It is presenting fine, but when the cancel button is clicked to dismiss the view it crashed with the message:

Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now

1

There are 1 best solutions below

0
On

I think this is easy to fix, the difficult part is to know where to fix it.

You give only a code snippet which makes it more difficult. Here are two attempts.

Try changing your code as below. The stuff wrapped inside the dispatch_async is all that changed.

    UIPrintInfo *pi = [UIPrintInfo printInfo];
    pi.outputType = UIPrintInfoOutputGeneral;

    NSString *url=[[req URL] absoluteString];
    pi.jobName = url;

    pi.orientation = UIPrintInfoOrientationPortrait;
    pi.duplex = UIPrintInfoDuplexLongEdge;

    dispatch_async( dispatch_get_main_queue (), ^{

        UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
        pic.printInfo = pi;
        pic.delegate=self;
        pic.showsPageRange = YES;
        pic.printFormatter = self.webView.viewPrintFormatter;


        [pic presentFromRect:CGRectMake(64, 64, self.view.frame.size.width,  self.view.frame.size.height-64) inView:self.view animated:YES completionHandler:^(UIPrintInteractionController* pic2, BOOL completed, NSError* error) {

        }];

    } );

I am guessing you do stuff inside the completion handler, so below is another attempt making sure the stuff gets done on the main thread. I think it is overkill though but depends on how you treat stuff.

    UIPrintInfo *pi = [UIPrintInfo printInfo];
    pi.outputType = UIPrintInfoOutputGeneral;

    NSString *url=[[req URL] absoluteString];
    pi.jobName = url;

    pi.orientation = UIPrintInfoOrientationPortrait;
    pi.duplex = UIPrintInfoDuplexLongEdge;

    dispatch_async( dispatch_get_main_queue (), ^{

        UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
        pic.printInfo = pi;
        pic.delegate=self;
        pic.showsPageRange = YES;
        pic.printFormatter = self.webView.viewPrintFormatter;

        [pic presentFromRect:CGRectMake(64, 64, self.view.frame.size.width,  self.view.frame.size.height-64) inView:self.view animated:YES completionHandler:^(UIPrintInteractionController* pic2, BOOL completed, NSError* error) {

            dispatch_async( dispatch_get_main_queue (), ^{
                ** stuff **
            } );

        }];

    } );