目前,对我来说,单击一个调用window.print()的链接只会触发decisionPolicyForNavigationAction委托方法,但我找不到任何相关内容.
解决方法 在发布之后(显然)想出来……希望这有助于其他人.创建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) } }}总结
以上是内存溢出为你收集整理的ios8 – 从WKWebView捕获window.print()全部内容,希望文章能够帮你解决ios8 – 从WKWebView捕获window.print()所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)