【问题标题】:AirPrint: Set default printer in UIPrintInteractionControllerAirPrint:在 UIPrintInteractionController 中设置默认打印机
【发布时间】:2012-10-19 01:59:28
【问题描述】:

我需要一种方法,使用已知的打印机 ID 将选定的打印机强制显示到 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
 }
}

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

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

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

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

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

我猜问题是打印机 ID 中出现了奇怪的字符。

有谁知道printInfo.printerId是怎么编码的,怎么手动设置的?

如果我将 NSString* printerId 存储到 ivar 中并在下一次打印操作时再次设置它,它可以工作,但我无法手动通过打印机 ID 强制使用默认打印机。


btw:显然,如果打印机不可用/无法访问,我知道无法选择...

【问题讨论】:

    标签: objective-c ios printing airplay


    【解决方案1】:

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

    【讨论】:

    • 谢谢,它适用于标准 AirPrint 打印机。问题在于 Printopia 附加了一个用作打印机图标的特殊字符。幸运的是可以在 Printopia 设置中禁用它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-07
    • 2020-03-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多