UIPrintInteractionController 仅在 iOS 14 上单击“取消”按钮时崩溃

Posted

技术标签:

【中文标题】UIPrintInteractionController 仅在 iOS 14 上单击“取消”按钮时崩溃【英文标题】:UIPrintInteractionController crashed on "Cancel" button clicked on iOS 14 only 【发布时间】:2020-10-13 09:56:47 【问题描述】:

我的代码是:

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) 
    
];
    
);

它显示得很好,但是当单击取消按钮以关闭视图时,它会因消息而崩溃:

试图从主线程或web线程以外的线程获取web lock。这可能是从辅助线程调用 UIKit 的结果。现在崩溃了

【问题讨论】:

【参考方案1】:

我认为这很容易解决,困难的部分是知道在哪里解决它。

你只给了一个代码 sn-p 这使它更难。这里有两个尝试。

尝试如下更改您的代码。包裹在 dispatch_async 中的东西完全变了。

    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) 

        ];

     );

我猜你在完成处理程序中执行stuff,所以下面是另一个确保stuff 在主线程上完成的尝试。我认为这有点矫枉过正,但取决于你如何对待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 **
             );

        ];

     );

【讨论】:

以上是关于UIPrintInteractionController 仅在 iOS 14 上单击“取消”按钮时崩溃的主要内容,如果未能解决你的问题,请参考以下文章