UIPrintInteractionController 总是显示当前的 WKWebView 而不是 iFrame 内容

Posted

技术标签:

【中文标题】UIPrintInteractionController 总是显示当前的 WKWebView 而不是 iFrame 内容【英文标题】:UIPrintInteractionController always shows current WKWebView instead of iFrame content 【发布时间】:2019-05-30 18:36:04 【问题描述】:

我有两个网页:/invoice_display,其中显示了一张发票并有一个“打印”按钮。 “打印”按钮创建一个隐藏的 iFrame 并将源设置为 /invoice_printable,其中嵌入了一个 window.print() 在页面加载时执行的页面中。

这一切在网络浏览器中运行良好,但在 ios 上加载 WKWebView 时出现问题。问题是UIPrintInteractionController 预览总是显示/invoice_display 而不是/invoice_printable

这是当前代码:

/invoice_printable

<script type="text/javascript">
    window.onload = function () 
        window.print();
    
</script>

UIViewController

- (void)createWebView 
    WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
    WKUserScript *script = [[WKUserScript alloc] initWithSource:@"window.print = function()  window.webkit.messageHandlers.print.postMessage('print') "
                                                  injectionTime:WKUserScriptInjectionTimeAtDocumentEnd
                                               forMainFrameOnly:NO];
    [config.userContentController addUserScript:script];
    [config.userContentController addScriptMessageHandler:self name:@"print"];

    WKWebView *webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:config];
    webView.navigationDelegate = self;
    webView.UIDelegate = self;

    [self.view addSubview:webView];
    self.webView = webView;


- (void)printCurrentPage 
    UIPrintInteractionController *printInteractionController = [UIPrintInteractionController sharedPrintController];
    printInteractionController.printFormatter = self.webView.viewPrintFormatter;
    UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) 
        if (!completed) 
            if (error) 
                NSLog(@"Print failed: %@", error);
             else 
                NSLog(@"Print cancelled");
            
        
    ;

    [printInteractionController presentAnimated:YES completionHandler:completionHandler];


- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message 
    if ([message.name isEqualToString:@"print"]) 
        NSLog(@"Got a 'print' message from the web page");
        [self printCurrentPage];
     else 
        NSLog(@"Got a web page message other than 'print'");
    

如何让可打印的 iFrame 内容显示在 UIPrintInteractionController 而不是原始网页中?

【问题讨论】:

【参考方案1】:

我想出了一个解决方案。我正在使用webView:decidePolicyForNavigationAction:decisionHandler: 来检测 iFrame。然后我使用 iFrame 的NSURLRequest 将网页作为NSData 获取,然后我将其作为NSString 传递给UIMarkupTextPrintFormatter。最后,该格式化程序被传递给UIPrintInteractionController。呼。

这是工作代码:

-(void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler 
    if (!navigationAction.targetFrame.isMainFrame) 
        NSLog(@"Got an iFrame: %@", navigationAction.request.URL.absoluteString);
        // Save this request so we can use it later for invoice printing
        self.printRequest = navigationAction.request;
        decisionHandler(WKNavigationActionPolicyAllow);
    

然后一旦网页调用window.print()...

- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message 
    if ([message.name isEqualToString:@"print"]) 
        NSLog(@"Got a 'print' message from the web page");
        [self GETWebDataWithRequest:self.printRequest];
     else 
        NSLog(@"Got a web page message other than 'print'");
    


- (void)GETWebDataWithRequest:(NSURLRequest *)request 
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:nil delegateQueue:nil];
    NSURLSessionDataTask *getDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) 
        if(!error) 
            NSLog(@"Got web data for url: %@", request.URL);
            dispatch_async(dispatch_get_main_queue(), ^
                [self printCurrentPageWithData:data];
            );
         else 
            NSLog(@"Web data error: %@", error);
        
    ];

    [getDataTask resume];


- (void)printCurrentPageWithData:(NSData *)webData 
    NSString *htmlString = [[NSString alloc] initWithData:webData encoding:NSUTF8StringEncoding];
    UIMarkupTextPrintFormatter *markupTextPrintFormatter = [[UIMarkupTextPrintFormatter alloc] initWithMarkupText:htmlString];
    UIPrintInteractionController *printInteractionController = [UIPrintInteractionController sharedPrintController];
    printInteractionController.printFormatter = markupTextPrintFormatter;

    UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) 
        if (!completed) 
            if (error) 
                NSLog(@"Print failed: %@", error);
             else 
                NSLog(@"Print cancelled");
            
        
    ;

【讨论】:

以上是关于UIPrintInteractionController 总是显示当前的 WKWebView 而不是 iFrame 内容的主要内容,如果未能解决你的问题,请参考以下文章