使用iOS AirPrint 讓你的APP輕松實現(xiàn)打印功能
1, 什么是AirPrint
其實就是將iOS(iphone,ipad)上的內(nèi)容,使用支持AirPrint的打印機打印出來。打印過程無線控制, 非常方便。
2, 第一手資料
學(xué)習(xí)iOS, 第一手資料肯定非蘋果官方文檔莫屬.here。 (我下面敘述的內(nèi)容基本上是對文檔的總結(jié), 英語可以的建議直接看文檔。。。)
3, Printer Simulator,使用打印模擬器進行測試
既然涉及打印功能,那么就需要有一臺支持AirPrint 功能的打印機進行測試嘍,你沒有?沒關(guān)系!蘋果已經(jīng)為我們準備好了模擬器。 這個模擬器在Xcode中沒有, 需要自己到官網(wǎng)下載:下載Printer Simulator(需要先注冊登錄)


4、打印代碼
協(xié)議UIPrintInteractionControllerDelegate
主要是用到了UIPrintInteractionController類,這是一個單列類。UIPrintInfo是配置打印信息,UISimpleTextPrintFormatter是設(shè)置頁面范圍。
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
? ? ? ? ? ? ? ? if? (pic && [UIPrintInteractionControllercanPrintData:data]) {
? ? ? ? ? ? ? ? ? ? pic.delegate=self;
? ? ? ? ? ? ? ? ? ? UIPrintInfo*printInfo = [UIPrintInfoprintInfo];
? ? ? ? ? ? ? ? ? ? printInfo.outputType=UIPrintInfoOutputGeneral;
//? ? ? ? ? ? ? ? ? ? printInfo.jobName = [self.path lastPathComponent];
? ? ? ? ? ? ? ? ? ? printInfo.duplex=UIPrintInfoDuplexLongEdge;
? ? ? ? ? ? ? ? ? ? pic.printInfo= printInfo;
? ? ? ? ? ? ? ? ? ? pic.showsPageRange=YES;
? ? ? ? ? ? ? ? ? ? pic.printingItem= data;
? ? ? ? ? ? ? ? ? ? void(^completionHandler)(UIPrintInteractionController*,BOOL,NSError*) =
? ? ? ? ? ? ? ? ? ? ^(UIPrintInteractionController*pic,BOOLcompleted,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) {
? ? ? ? ? ? ? ? ? ? ? ? [picpresentFromBarButtonItem:self.saveButtonanimated:YES
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? completionHandler:completionHandler];
? ? ? ? ? ? ? ? ? ? }else{
? ? ? ? ? ? ? ? ? ? ? ? [picpresentAnimated:YEScompletionHandler:completionHandler];
? ? ? ? ? ? ? ? ? ? }
Info.plist文件中的第一項 Localization native development region(CFBundleDevelopmentRegion)的值設(shè)為 China(zh_CN);

