iOS print without allowing UIPrintInteractionController to appear
Asked Answered
T

2

7

I am researching a potential app for a client, and I had a question I wasn't able to find the answer on. What I would like to do is print automatically, without having the UIPrintInteractionController get displayed. An important point I should make is this will not be a production app. I know that otherwise this could violate the user's privacy to just print without the user's consent, but again this will not be a public app on the App Store. One app I found that seems to be able to this is Printer Pro. The user is prompted to print a test page, and the page prints without the UIPrintInteractionController appearing - this app is on the store, so I'm assuming there's a legal way to do it. Anyone know how this is done?

Tenancy answered 30/10, 2012 at 18:47 Comment(2)
Ever get an answer to this?Gladiatorial
Unfortunately, no. Here is a relevant link #5136281Tenancy
F
7

As far as I know it is not possible to print in iOS7 without invoking the UIPrintInteractionController and displaying the system print dialog.

However, iOS8 now provides a provision for printing directly to a print without showing the dialog. The basic idea is that you obtain a UIPrinter object and use this in conjunction with the new - printToPrinter:completionHandler: method of the UIPrintInteractionController to print without showing the dialog.

Obtaining the UIPrinter object for your printer can be accomplished in a couple of ways.

The most straightforward is to use the new UIPrinterPickerController. But this would show a dialog and so doesn’t meet your needs.

The alternative is to create a printer via its URL using the UIPrinter method + printerWithURL:. I’m not entirely clear how to obtain this URL but it may be contained in the printer’s HTML admin pages. And I believe you can obtain it programmatically using the Bonjour API. More info here:

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/NetServices/Introduction.html#//apple_ref/doc/uid/10000119i

Bonjour is a service discovery API which includes discovery of IPP printers, which the is the protocol used by AirPrint.

As for how the Printer Pro app is able to print without a dialog in iOS7, I would guess they are interacting with the printer at a very low level (e.g. raw HTTP posts, etc.).

Flavone answered 8/10, 2014 at 17:56 Comment(2)
This works fine, seemingly except for printers shared by cups (ie. on ubuntu). For example I cannot print to either of my 2 printers shared with ubuntu/CUPS via printToPrinter: in my app, but my app can print directly to my 2 Canon CP910 printers. At the same time, the native Photos app can print to my 2 printers shared by ubuntu/CUPS. So not everything works with printToPrinter:Caldwell
Update: the workaround i found is to store both the URL to the printer and a strong reference to the UIPrinter as well. If the URL cannot be connected to, try the UIPrinter directly. Alarmingly this works... looks like an iOS 8 bug since there is no reason why the URL can't be used AFAIKCaldwell
N
3

Follow these steps to Print the Documents without prompting..

First Search for Devices Using the Below Code...

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
    UIPrinterPickerController *printPicker = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];

    [printPicker presentAnimated:YES completionHandler:
        ^(UIPrinterPickerController *printerPicker, BOOL userDidSelect, NSError *error) {

            if (userDidSelect) {
                //User selected the item in the UIPrinterPickerController and got the printer details.

                [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printerPicker.selectedPrinter];

                // Here you will get the printer and printer details.ie,
                // printerPicker.selectedPrinter, printerPicker.selectedPrinter.displayName, printerPicker.selectedPrinter.URL etc. So you can display the printer name in your label text or button title.

                NSURL *printerURL = printerPicker.selectedPrinter.URL;

                NSLog(@"printerURL--->%@",printerURL.absoluteString);

                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:[printerURL absoluteString] forKey:@"printURL"];
                [defaults synchronize];
            }
    }];
}

And Print the Documents by without prompting by using the below code...

if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1) {
   NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

   UIPrinter *currentPrinterObj = [UIPrinter printerWithURL:[NSURL URLWithString:[defaults stringForKey:[defaults stringForKey:@"printURL"]]]];

   UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];

   if(currentPrinterObj) {
        [controller printToPrinter:currentPrinterObj completionHandler:^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
            if(completed) {

            } else {
                NSLog(@"FAILED! due to error in domain %@ with error code %u", error.domain, error.code);
            }
        }];
    }
}
Naoise answered 15/11, 2016 at 12:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.