
作者:dustturtle
前言:AirPrint 技術問世已久,但傳統實現方式下,每次打印都需要用戶在客戶端點擊選擇打印機並確認,流程頗為繁瑣。所幸蘋果在 iOS 8 中對其做了更新,使 iOS 設備得以支持無交互的後台打印。本文將介紹無交互打印的流程、原理與具體實現,並附上源代碼。
關於 AirPrint
AirPrint 是蘋果提出的一套無驅動打印體系架構,讓應用可以生成無損的打印輸出。所有支持打印的 iOS 內建 app 都基於 AirPrint,App Store 上調用 iOS 打印系統的 app 也同樣如此,而官方 AirPrint 打印機和服務器均經過蘋果許可與認證。(以上文字來自百度百科)
簡單來說,AirPrint 就是蘋果定義的一種通用規範或標準,符合規範的打印機能直接連接 iOS 設備打印,無需安裝驅動。而對客戶端而言,只需調用蘋果 AirPrint 的相關 API 即可連接打印,無需集成各家廠商 SDK,極大簡化了開發。
AirPrint 的簡單實現(iOS 8 之前的方案)
主要代碼如下:
- (IBAction)airPrint:(id)sender { UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController]; if(!controller) { NSLog(@"Couldn't get shared UIPrintInteractionController!"); return; } controller.delegate = self; UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputPhoto; printInfo.orientation = UIPrintInfoOrientationPortrait; // UIPrintInfoOrientationPortrait or UIPrintInfoOrientationLandscape printInfo.jobName = @"AirPrintWechatSize"; controller.printInfo = printInfo; UIImage *printImage = [UIImage imageNamed:@"7.jpg"]; controller.printingItem = printImage; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) { }; if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) { //iPad [controller presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler]; } else { [controller presentAnimated:YES completionHandler:completionHandler]; } }iOS 8 之前方案的步驟與缺陷
主要步驟如下:
1. 獲取 UIPrintInteractionController
2. 設置 Print Info
3. 設置 printingItem
4. 設置打印回調
5. 彈出 UIPrintInteractionController
缺陷在於,每次打印都要彈出 UIPrintInteractionController 並在其中選擇打印機、確認打印。但在很多場景下,我們更希望這一切能在後台默默完成,讓程序更智能、交互體驗更出色。
Print without UI
首先是選擇並保存打印機信息的代碼:
UIPrinterPickerController *pickerController =[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil]; CGRect rect; UIInterfaceOrientation currentOrientation = [UIApplication sharedApplication].statusBarOrientation; rect = CGRectMake(320, 130, 0, 0); void (^completionHandler)(UIPrinterPickerController *, BOOL, NSError *) = ^(UIPrinterPickerController *printController, BOOL userDidSelect, NSError * err) { if (userDidSelect) { // save the urlString and Printer name, do your UI interactions GSignInConfig.airPrinterUrlStr = controller.selectedPrinter.URL.absoluteString; GSignInConfig.airPrinterName = controller.selectedPrinter.displayName; } };UIPrinterPickerController 需要 iOS 8+;它的設計理念是一次用戶交互就把打印機信息從系統傳遞到客戶端,之後客戶端即可用這些信息進行打印。其中關鍵是 controller.selectedPrinter.URL.absoluteString,有了它就能定位打印機(前提是打印機與 iOS 設備的網絡連接情況沒有變化)。GSignInConfig 是我自己實現的配置持久化接口,可簡單理解為 userdefault,這裡持久化了打印機的 absoluteString 和 displayName。
[ [UIPrinter printerWithURL:[NSURL URLWithString:printerUrlStr]] contactPrinter:^(BOOL available) { if (available) { DDLogInfo(@"AIRPRINTER AVAILABLE"); } else { DDLogInfo(@"AIRPRINTER NOT AVAILABLE"); } }]; 上面的代碼實現了打印機連接狀態的後台檢查,其中 printerUrlStr 就是此前獲取並保存的 absoluteUrl,用它便能構建出一個 UIPrinter 對象。這一步需要在實際打印前完成。
- (void)startAirPrintWithImage:(UIImage *)image { UIPrintInfo *printInfo = [UIPrintInfo printInfo]; printInfo.outputType = UIPrintInfoOutputGeneral; printInfo.orientation = UIPrintInfoOrientationPortrait; printInfo.jobName = @"CoolVisitAirPrint"; self.airPrinterController.printInfo = printInfo; self.airPrinterController.printingItem = image; self.airPrinterController.delegate = self; void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error){ if(completed && error) DDLogError(@"Printing failed due to error in domain %@ with error code %lu. Localized description: %@, and failure reason: %@", error.domain, (long)error.code, error.localizedDescription, error.localizedFailureReason); }; UIPrinter *airPrinter = [UIPrinter printerWithURL:[NSURL URLWithString:GSignInConfig.airPrinterUrlStr]]; [self.airPrinterController printToPrinter:airPrinter completionHandler:completionHandler]; }檢查完畢後,即可調用上面的代碼完成打印。同樣地,這裡的 UIPrinter 對象也是由之前保存的 absoluteString 構建而成。
打印相關的代理方法可見 UIPrintInteractionControllerDelegate,它能控制打印過程中的流程與狀態,並隨 UIPrintInteractionController 一同提供,iOS 8 之前的版本也都能使用。
參考鏈接:AirPrint 蘋果官方參考資料
(完)