5

Is there a way to detect window.print from a UIWebView and respond to it? I have printing in my UiWebView and need to have a way to react to that event from the UIWebView.

2
  • Can you add more details on what "react" means here? And what is the expected handle for it to work? Do you need a Javascript handle? or a native handle? Let me know I have a solution in mind, but not sure because not clear with expected result.
    – aravind
    Commented Mar 19, 2014 at 18:40
  • I want to be able to intercept window.print and then use native code to open a print dialog Commented Mar 19, 2014 at 21:24

1 Answer 1

11
+200

First, this is the solution I came across. But UIWebview does not support those events and the API like in a desktop browser. And haven't yet got the matchMedia to work. Probably will work when I have a printer configured!?!.

The actual reason is that the media type of the web page did not change from screen to print in the UIWebView, unlike desktop browsers, so there is no trigger, till this point.

And UIWebView has no delegate method for printing exposed till date.

Anyways, where there is a will, there is a [hack] way. Two step solution.

Step 1. Javascript

If you have access to the HTML or JS file, and it is used only for this UIWebView, you can add the below lines to the JS

(function(){
    var originalPrintFn = window.print; // Keep it cached in this variable, just in case
    window.print = function(){
        // Trigger location change
        window.location = "your_app_scheme:print";
    }
})();

If you don't have access to the HTML or JS file, in your UIWebView delegate, add the below code

[self.myWebView stringByEvaluatingJavaScriptFromString:@"(function(){var originalPrintFn = window.print;window.print = function(){window.location = 'your_app_scheme:print';}})();"];

This can be done in the delegate's webViewDidFinishLoad method

Step 2. Catching the call in native

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    if ([[[request URL] absoluteString] hasPrefix:@"your_app_scheme:"]) {

        // Call the given selector
        [self performSelector:@selector(your_handle_to_print)];        
        // Cancel the event, so the webview doesn't load the url
        return NO;
    }
    return YES;
}

After this in your_handle_to_print, you can do your printing magic.

Hope this helps!

5
  • Is there a way to support regular print and ios print using the same button? Or should I just show/hide button based on if I detect they are using native app? Commented Mar 20, 2014 at 1:27
  • @ChrisMuench Yeah that should be possible. By following the [self.myWebView stringByEvaluatingJavaScriptFromString:@"..."]; method instead of changing in HTML/JS file. So that way, when the web page opens in Mobile Safari or other browsers, the print button shows up normal print dialog. In your native UIWebView app, the method is overridden, so you can handle that
    – aravind
    Commented Mar 20, 2014 at 4:29
  • I am not quite following. Do you want me to run js from ios app to show/hide button that has on click? Commented Mar 22, 2014 at 15:03
  • Run this portion from ios native [self.myWebView stringByEvaluatingJavaScriptFromString:@"(function(){var originalPrintFn = window.print;window.print = function(){window.location = 'your_app_scheme:print';}})();"]; . It'll override the print only inside your app's UIWebview. If the same HTML page is opened on any other browser, it will produce the default functionality, i.e. print dialog.
    – aravind
    Commented Mar 22, 2014 at 15:56
  • Does anyone know the Android equivalent for this? Commented Apr 3, 2018 at 16:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.