使用iOS AirPrint 讓你的APP輕松實現(xiàn)打印功能
1, 什么是AirPrint
其實就是將iOS(iphone,ipad)上的內(nèi)容仍侥,使用支持AirPrint的打印機(jī)打印出來芝此。打印過程無線控制, 非常方便假褪。
2, 第一手資料
學(xué)習(xí)iOS, 第一手資料肯定非蘋果官方文檔莫屬.here款票。 (我下面敘述的內(nèi)容基本上是對文檔的總結(jié), 英語可以的建議直接看文檔漱牵。。疚漆。)
3, Printer Simulator,使用打印模擬器進(jìn)行測試
既然涉及打印功能酣胀,那么就需要有一臺支持AirPrint 功能的打印機(jī)進(jìn)行測試嘍,你沒有愿卸?沒關(guān)系灵临!蘋果已經(jīng)為我們準(zhǔn)備好了模擬器。 這個模擬器在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);