AirPrint:在UIPrintInteractionController中设置默认打印机

Lub*_*bbo 5 printing objective-c ios airplay

我需要一种方法,使用已知的printerId将选定的打印机强制显示到UIPrintInteractionController.

注意:要进行测试我在我的"MacBook Pro"上安装了Printopia,共享"打印机"

我做了这个测试:

-(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
 }
}
Run Code Online (Sandbox Code Playgroud)

打印完成后,应用程序将记录以下打印机ID:

?\032Printer\032@\032MacBook\032Pro._ipp._tcp.local.
Run Code Online (Sandbox Code Playgroud)

我想覆盖打印机,所以我应该这样做:

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.printerId = @"?\032Printer\032@\032MacBook\032Pro._ipp._tcp.local.";
controller.printInfo = printInfo;
Run Code Online (Sandbox Code Playgroud)

但由于某些原因它不起作用,UIPrintInteractionController不按预期选择打印机,但打印机显示在打印机列表中.

我想问题是打印机ID中存在奇怪的字符.

任何人都知道如何编码printInfo.printerId以及如何手动设置它?

如果我将NSString*printerId存储到ivar并在下一次打印操作时再次设置它可以工作,但我不能手动强制打印机Id的默认打印机.


顺便说一句:显然,如果打印机不可用/无法访问,我知道无法选择...

Sil*_*les 7

为了以编程方式设置默认打印机,您只需将printInfo的printerID设置为._ipp._tcp.local.printerName应与在UIPrinterInteractionController弹出窗口中的打印机列表中显示的方式完全相同.例如,对于显示为LANIERCOLOR315 [00:80:A3:95:2D:41]的打印机,printerID为LANIERCOLOR315 [00:80:A3:95:2D:41]._ipp._tcp.local.您不必编码特殊字符.该框架将做到这一点.