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.
-
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 Mar 19 '14 at 18:40
-
I want to be able to intercept window.print and then use native code to open a print dialog – Chris Muench Mar 19 '14 at 21:24
1 Answers
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!
-
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? – Chris Muench Mar 20 '14 at 01: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 Mar 20 '14 at 04:29
-
I am not quite following. Do you want me to run js from ios app to show/hide button that has on click? – Chris Muench Mar 22 '14 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 Mar 22 '14 at 15:56
-