Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何从WKWebView打印特定iFrame_Ios_Iframe_Wkwebview - Fatal编程技术网

Ios 如何从WKWebView打印特定iFrame

Ios 如何从WKWebView打印特定iFrame,ios,iframe,wkwebview,Ios,Iframe,Wkwebview,我正在使用与下面链接的问题类似的技术捕获window.print()并触发WKWebView的打印。这对于主页来说很好 我希望在页面中打印iFrame,而不是页面本身。在JavaScript中,我关注iFrame和call window.print(),它在iOS上的Safari中工作(它使用WKWebView?),以及各种桌面浏览器 有人知道如何使用WKWebView printFormatter打印HTML内容的iFrame吗?在过去的3天里,我一直在为同样的问题苦苦挣扎,并在网上拼命寻找

我正在使用与下面链接的问题类似的技术捕获window.print()并触发WKWebView的打印。这对于主页来说很好

我希望在页面中打印iFrame,而不是页面本身。在JavaScript中,我关注iFrame和call window.print(),它在iOS上的Safari中工作(它使用WKWebView?),以及各种桌面浏览器


有人知道如何使用WKWebView printFormatter打印HTML内容的iFrame吗?

在过去的3天里,我一直在为同样的问题苦苦挣扎,并在网上拼命寻找解决方案,但我没有找到

但我终于让它运行起来了,下面是我的解决方案:

  • 将以下用户脚本添加到WKWebViewconfig.userContentController:

     WKUserScript *printScript = [[WKUserScript alloc] initWithSource:@"window.print = function() {window.webkit.messageHandlers.print.postMessage('print')}"
                                                  injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
    
    [webViewConfig.userContentController addUserScript:printScript];
    [webViewConfig.userContentController addScriptMessageHandler:self name:@"print"];
    
    -(void) userContentController:(WKUserContentController     *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
    
    {
     if([message.name isEqualToString:@"print"])
    {
        [self.webView evaluateJavaScript:@"window.document.querySelector(\"iframe\").src" completionHandler:^(NSString *result, NSError *error)
        {
            UIPrintInteractionController    *printController = UIPrintInteractionController.sharedPrintController;
            printController.printingItem = [NSURL URLWithString:result];
    
            UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
            {
                if(!completed)
                {
                    if(error != nil) NSLog(@"Print failed: \nDomain: %@\nError code:%ld", error.domain, (long) error.code);
                    else             NSLog(@"Print canceled");
                }
                NSLog(@"Print completed!\nSelected Printer ID: %@", printController.printInfo.printerID);
            };
    
            if(printController != nil)
                [printController presentAnimated:YES completionHandler:completionHandler];
        }];
    }
    
    else
        NSLog(@"Unrecognized message received from web page");    }
    
  • =>这与中相同,但有一个关键区别:FormInframeOnly设置为NO,因为我们希望将其注入iframe,而不仅仅是顶部窗口

  • 在保存WKWebview的视图控制器中定义消息处理程序:
  • 在标题中:

    @interface MyViewcontroller : UIViewController <UIGestureRecognizerDelegate, WKNavigationDelegate, WKScriptMessageHandler>
    
  • 使用消息处理程序检索iframe的源(即您的pdf),并将其用作iOS的PrintController的打印项:

     WKUserScript *printScript = [[WKUserScript alloc] initWithSource:@"window.print = function() {window.webkit.messageHandlers.print.postMessage('print')}"
                                                  injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:NO];
    
    [webViewConfig.userContentController addUserScript:printScript];
    [webViewConfig.userContentController addScriptMessageHandler:self name:@"print"];
    
    -(void) userContentController:(WKUserContentController     *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message
    
    {
     if([message.name isEqualToString:@"print"])
    {
        [self.webView evaluateJavaScript:@"window.document.querySelector(\"iframe\").src" completionHandler:^(NSString *result, NSError *error)
        {
            UIPrintInteractionController    *printController = UIPrintInteractionController.sharedPrintController;
            printController.printingItem = [NSURL URLWithString:result];
    
            UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error)
            {
                if(!completed)
                {
                    if(error != nil) NSLog(@"Print failed: \nDomain: %@\nError code:%ld", error.domain, (long) error.code);
                    else             NSLog(@"Print canceled");
                }
                NSLog(@"Print completed!\nSelected Printer ID: %@", printController.printInfo.printerID);
            };
    
            if(printController != nil)
                [printController presentAnimated:YES completionHandler:completionHandler];
        }];
    }
    
    else
        NSLog(@"Unrecognized message received from web page");    }
    
  • 注意:我使用
    window.document.querySelector(\“iframe\”)
    ,因为我们的iframe没有id,否则我会使用
    window.getComponentByID
    。如果您有多个iframe,这一点很重要,但我的情况不是这样

    我希望这对你和其他人都有帮助