【问题标题】:Capturing window.print() from a WKWebView从 WKWebView 捕获 window.print()
【发布时间】:2014-11-16 09:31:33
【问题描述】:

我正在尝试使用我的应用程序从 WKWebView 捕获 window.print() javascript 调用,以实际显示打印对话框并允许它们从页面上显示的按钮(而不是我的应用程序内的按钮)进行打印.有谁知道完成此任务的最佳方法?

目前,对我来说,单击调用 window.print() 的链接只会触发 decisionPolicyForNavigationAction 委托方法,但我在其中找不到任何相关内容。

【问题讨论】:

  • 请问您打印的是哪个视图?
  • 我正在打印 WKWebView 的 viewPrintFormatter

标签: ios8 wkwebview wkwebviewconfiguration


【解决方案1】:

在发布后(显然)想通了......希望这对其他人有所帮助。

创建 Web 视图时...

let configuration = WKWebViewConfiguration()
let script = WKUserScript(source: "window.print = function() { window.webkit.messageHandlers.print.postMessage('print') }", injectionTime: WKUserScriptInjectionTime.AtDocumentEnd, forMainFrameOnly: true)
configuration.userContentController.addUserScript(script)
configuration.userContentController.addScriptMessageHandler(self, name: "print")
self.webView = WKWebView(frame: CGRectZero, configuration: configuration)

然后只需在你的视图控制器中采用 WKScriptMessageHandler 协议并添加所需的方法...

func userContentController(userContentController: WKUserContentController, didReceiveScriptMessage message: WKScriptMessage) {
    if message.name == "print" {
        printCurrentPage()
    } else {
        println("Some other message sent from web page...")
    }
}

func printCurrentPage() {
    let printController = UIPrintInteractionController.sharedPrintController()
    let printFormatter = self.webView.viewPrintFormatter()
    printController?.printFormatter = printFormatter

    let completionHandler: UIPrintInteractionCompletionHandler = { (printController, completed, error) in
        if !completed {
            if let e = error? {
                println("[PRINT] Failed: \(e.domain) (\(e.code))")
            } else {
                println("[PRINT] Canceled")
            }
        }
    }

    if let controller = printController? {
        if UIDevice.currentDevice().userInterfaceIdiom == .Pad {
            controller.presentFromBarButtonItem(someBarButtonItem, animated: true, completionHandler: completionHandler)
        } else {
            controller.presentAnimated(true, completionHandler: completionHandler)
        }
    }
}

【讨论】:

  • iPad 的self.browserController.addressBarActivityButton 是什么?
  • 在 iPad 上,您必须从按钮/视图中显示打印控制器。这只是我要展示的一个条形按钮项目。从我的项目中复制代码留下的。
【解决方案2】:

有一个 undocumented 委托用于挂钩 window.print()s

class MyApp: WKUIDelegate {
  func makeWebview() {
      ...
      self.webview.UIDelegate = self
  }
  func _webView(webView: WKWebView!, printFrame: WKFrameInfo) {
      println("JS: window.print()")
      printCurrentPage()
  }     
}

【讨论】:

  • 似乎很奇怪,他们公开了用于创建新窗口的委托方法,但将这个方法保密。希望他们在未来改变这一点。如果您不需要您的应用获得苹果的批准,这可能是一个不错的选择。
  • WebView“私有”方法并不是那么私有,很多人在 App Store 提交中使用它们,甚至在将它们提升为公共方法后支持带下划线的函数签名。例如,Apple 在 iOS9/OSX10.11 中为 WKWebView 添加了一个新的私有方法:_printOperationWithPrintInfo:github.com/WebKit/webkit/commit/…
猜你喜欢
  • 2010-09-18
  • 1970-01-01
  • 1970-01-01
  • 2018-05-13
  • 2018-07-18
  • 1970-01-01
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
相关资源
最近更新 更多