Fork me on GitHub

在应用里加入 AirPrint 功能的方法

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

 

posted on 2012-05-15 20:10  pengyingh  阅读(349)  评论(0)    收藏  举报

导航