Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AirPrint: Set default printer in UIPrintInteractionController

I need a way to force the selected Printer to the UIPrintInteractionController when it's presented, using an already known printerId.

NOTE: To make tests I'm using Printopia installed on my "MacBook Pro" that shares the "Printer"

I've made this test:

-(IBAction)print:(id)sender
{
 UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];

 UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
      NSLog(@"Selected Printer ID: %@",printController.printInfo.printerID);      
  };

 NSString* path = [[NSBundle mainBundle] pathForResource:@"TestImage" ofType:@"png"];
 NSURL* imageURL = [NSURL fileURLWithPath:path isDirectory:NO];

 UIPrintInfo *printInfo = [UIPrintInfo printInfo];
 printInfo.outputType = UIPrintInfoOutputPhoto;
 printInfo.jobName = @"Image print";
 controller.printInfo = printInfo;

 controller.printingItem = imageURL;

 if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) 
 {
    [controller presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];  // iPad
 }
 else
 {
     [controller presentAnimated:YES completionHandler:completionHandler];  // iPhone
 }
}

When the print is done, the app logs following Printer ID:

\032Printer\032@\032MacBook\032Pro._ipp._tcp.local.

I want to override the printer so I supposed to do in this way:

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.printerId = @"\032Printer\032@\032MacBook\032Pro._ipp._tcp.local.";
controller.printInfo = printInfo;

But for some reason it doesn't work, the UIPrintInteractionController don't select the printer as expected, but the printer is shown in the printer list.

I guess the problem is the strange chars present in the Printer ID.

Anyone knows how the printInfo.printerId is encoded and how to manually set it?

If I store the NSString* printerId into an ivar and sets it again on next print action it works, but I cannot force a default printer by printer Id by hand.


btw: Obviously, if the printer is not available/reachable, I know that cannot be selected ...

like image 917
Lubbo Avatar asked Oct 15 '12 15:10

Lubbo


1 Answers

In order for you to set the default printer programmatically, you only need to set the printerID of the printInfo to ._ipp._tcp.local. The printerName should be exactly the same as how it is being displayed in the list of printers in the UIPrinterInteractionController popover. For example for a printer that is being displayed as LANIERCOLOR315 [00:80:A3:95:2D:41], the printerID is LANIERCOLOR315 [00:80:A3:95:2D:41]._ipp._tcp.local. You do not have to encode the special characters. The framework will do it.

like image 199
Silver Gonzales Avatar answered Oct 02 '22 18:10

Silver Gonzales