首页 > 解决方案 > UIPrintInteractionController 仅在 iOS 14 上单击“取消”按钮时崩溃

问题描述

我的代码是:

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 的结果。现在崩溃

标签: objective-cuikitios14uiprintinteractioncntrler

解决方案


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

您只提供一个代码片段,这使得它变得更加困难。这里有两个尝试。

尝试如下更改您的代码。包裹在里面的东西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 **
            } );

        }];

    } );

推荐阅读