iPad / iPhone - 直接打印到网络打印机,无需使用airprint popover

时间:2011-02-27 20:36:45

标签: printing airprint

我正在开发一个iPad / iPhone应用程序,要求我在完成交易时在网络打印机上打印收据。我已经设法在某种程度上获得了airprint功能,因为我可以正确显示UIPrintInteractionController弹出窗口,单击“打印”按钮,然后在打印机模拟器中查看结果。由于我的应用程序的要求,我希望跳过popover步骤并在关闭事务时自动打印收据。换句话说,是否可以将打印作业发送到预先指定的网络打印机而无需添加额外的按钮单击?我是否需要尝试扩展UIPrintInteractionController类?如果是这样,有没有人有这种方法的运气?

任何其他替代建议也会很棒。

3 个答案:

答案 0 :(得分:1)

使用UIPrintInteractionController类无法做到这一点,它旨在为用户提供标准的打印选项,并且没有app store安全的解决方法。

答案 1 :(得分:0)

试试这个

  - (IBAction)Print:(id)sender {
    [self searchForPrinters];
   }
 - (void) searchForPrinters
  {

  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.

             [btnSettingsPrint setTitle:printerPicker.selectedPrinter.displayName forState:UIControlStateNormal];

             NSURL *printerURL = printerPicker.selectedPrinter.URL;

         }
     }];
}
 }

 -(void)printYourItem :(NSData*)data
 {
 if (NSFoundationVersionNumber > NSFoundationVersionNumber_iOS_7_1)
  {
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    UIPrinter *currentPrinterObj = [UIPrinter printerWithURL:[NSURL URLWithString:[defaults stringForKey:@"YouKeys"]]];

    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);
             }
         }];
    }
}
}

答案 2 :(得分:-1)

有一种方法

#import <BRPtouchPrinterKit/BRPtouchPrinterKit.h> 

BRPtouchPrinterKit是打印机兄弟的更多信息http://www.brother.com/product/dev/mobile/ios/

特别适用于此类打印机的SDK