AirPrint 是苹果 iOS 系统自带的无线打印功能,阅读、新闻等类型的应用内如能集成 AirPrint,会给消费者带来极大便利。CocoaChina 会员分享了在应用里加入 AirPrint 功能的方法,希望下面的代码能为相关应用的开发者们节省时间。

 

NSString *path = [[NSBundle mainBundle] pathForResource:@”test” ofType:@”jpg”];
NSData *data = [NSData dataWithContentsOfFile: path];

UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

if  (pic && [UIPrintInteractionController canPrintData: data] ) {

pic.delegate = self;

UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
printInfo.jobName = [path lastPathComponent];
printInfo.duplex = UIPrintInfoDuplexLongEdge;
pic.printInfo = printInfo;
pic.showsPageRange = YES;
pic.printingItem = data;

void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {

if (!completed && error)
NSLog(@”FAILED! due to error in domain %@ with error code %u”,error.domain, error.code);
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[pic presentFromBarButtonItem:self.printButton animated:YES completionHandler:completionHandler];
} else {
[pic presentAnimated:YES completionHandler:completionHandler];
}
}

 

相关文章:

  • 2021-06-17
  • 2021-10-22
  • 2022-02-25
  • 2022-12-23
  • 2022-01-19
  • 2021-04-04
  • 2021-09-07
  • 2021-11-18
猜你喜欢
  • 2022-12-23
  • 2021-08-14
  • 2021-11-13
  • 2021-10-06
  • 2021-10-18
  • 2021-08-22
  • 2021-10-28
相关资源
相似解决方案