WKWebView - 打开打印对话框

时间:2016-12-21 09:57:25

标签: swift cocoa wkwebview

我在旧的Cocoa项目中使用普通的WebView组件,可以覆盖

-(void)webView:(WebView *)sender printFrameView:(WebFrameView *)frameView

在必要时显示打印对话框。

如何使用WKWebView执行相同的操作?我无法在文档中找到任何有关打印的方法。

1 个答案:

答案 0 :(得分:0)

我有以下使用Swift在WKWebView上进行AirPrint的方法(例如,显示打印对话框)。它将弹出打印对话框,并根据需要在多页中正确设置文本格式。我从应用程序上的“打印”按钮调用printPage。

webViewX是WKWebView,在该类的其他地方定义:

var webViewX: WKWebView!
//do all the work for populating the webViewX somewhere else in the class

@objc func printPage(sender: AnyObject)
{
    let pInfo :UIPrintInfo =  UIPrintInfo.printInfo()
    pInfo.outputType = UIPrintInfo.OutputType.general
    pInfo.jobName = self.title //page title defined elsewhere
    pInfo.orientation = UIPrintInfo.Orientation.portrait

    let printController = UIPrintInteractionController.shared
    printController.printInfo = pInfo
    printController.printFormatter = webViewX.viewPrintFormatter()
    printController.present(animated: true, completionHandler: nil)
}