2014-11-16 2 views
4

내 애플 리케이션과 함께 WKWebView에서 window.print() 자바 스크립트 호출을 캡처하려고 실제로 인쇄 대화 상자를 표시하고 페이지에 표시된 버튼에서 인쇄 할 수있게하려고합니다. (내 앱 안에있는 버튼 대신에). 누구든지이 작업을 수행하는 가장 좋은 방법을 알고 있습니까?WKWebView에서 window.print() 캡처

현재 나를 위해 window.print()를 호출하는 링크를 클릭하면 아무것도 수행되지 않고 decidePolicyForNavigationAction 대리자 메서드가 실행되지 만 관련 항목을 찾을 수 없습니다.

+0

인쇄 할보기를 물어 볼 수 있습니까? – MobileOverlord

+0

WKWebView의 viewPrintFormatter를 인쇄하고 있습니다. –

답변

5

게시 한 직후에 알아 냈습니다. (분명히) ... 희망적으로 이것은 다른 사람에게 도움이되기를 바랍니다.

웹 뷰를 생성 ...

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) 

그럼 그냥

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) 
     } 
    } 
} 
+0

iPad 용 'self.browserController.addressBarActivityButton'은 무엇입니까? –

+1

iPad에서는 버튼 /보기에서 인쇄 컨트롤러를 제시해야합니다. 그건 내가 제시하는 바 버튼 항목 일 뿐이야. 내 프로젝트에서 코드 복사가 끝났습니다. –

0

가에 대한 undocumented 대표가 ...보기 컨트롤러에 WKScriptMessageHandler 프로토콜을 채택하고 필요한 방법을 추가 hooking window.print() s

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

새 창 만들기를 위임하는 방법을 공개했으나이 창을 비공개로 유지한다는 이상한 것처럼 보입니다. 다행히도 그들은 이것을 미래에 바꿀 것입니다. 애플이 애플의 승인을 필요로하지 않는다면 아마도 이것은 좋은 방법 일 것이다. –

+1

WebView의 "private"메소드는 너무 비공개가 아니기 때문에 많은 사람들이 App Store 제출물에서이 메소드를 사용하고 public 메소드로 승격시킨 후 밑줄 친 함수 서명을 지원합니다. 예를 들어, Apple은 iOS9/OSX10.11의 WKWebView에 새로운 개인 메서드를 추가했습니다. _printOperationWithPrintInfo : https://github.com/WebKit/webkit/commit/0dfc67a174b79a8a401cf6f60c02150ba27334e5 – kfix