日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

AirPrint: 无交互的后台打印实现(Print without UI,iOS8+)

發布時間:2023/12/31 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AirPrint: 无交互的后台打印实现(Print without UI,iOS8+) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言:AirPrint技術存在已經有了很長的時間,但是對于通常實現來說,每次打印都需要用戶在客戶端點擊選擇打印機并確認打印,流程上很不方便。所幸的是apple在iOS8更新了此技術,使其可以支持iOS設備上的無交互后臺打印。本文介紹了無交互打印的流程、原理和相關實現,并貼出源代碼。

關于AirPrint

AirPrint 是可以讓應用軟件通過 Apple 的無驅動程序打印體系結構,創建無損打印輸出的 Apple 技術。所有支持打印的 iOS 內建 app 均使用 AirPrint。App Store 上使用 iOS 打印系統的 App 也使用 AirPrint。官方 AirPrint 打印機和服務器經過 Apple 許可和認證。(以上文字來自百度百科)
簡單說來,airPrint就是蘋果定義的一種相對通用的規范或者說標準,滿足這種規范的打印機就可以直接連接iOS設備進行打印(無需安裝驅動)。而對于客戶端來說,只需要調用蘋果airPrint的相關API就可以實現連接airPrint打印機的打印,而無需集成各個廠商的sdk,大大方便的編程實現。

AirPrint打印簡單實現(iOS8以前的解決方案)

主要代碼如下:

- (IBAction)airPrint:(id)sender {UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];if(!controller){NSLog(@"Couldn't get shared UIPrintInteractionController!");return;}/* Set this object as delegate so you can use the printInteractionController:cutLengthForPaper: delegate */controller.delegate = self;/************************* Set Print Info ************************/UIPrintInfo *printInfo = [UIPrintInfo printInfo];/* Four types of UIPrintInfoOutputType you can assign to, and Paper Size Selection will be shown in the AirPrint View,those Paper Sizes to be shown also depends on what kind of OutputType you selected , and the locale of your iDevice */printInfo.outputType = UIPrintInfoOutputPhoto;/* Use landscape orientation for a banner so the text print along the long side of the paper. */printInfo.orientation = UIPrintInfoOrientationPortrait; // UIPrintInfoOrientationPortrait or UIPrintInfoOrientationLandscapeprintInfo.jobName = @"AirPrintWechatSize";controller.printInfo = printInfo;/******* Set Image to Print ***/UIImage *printImage = [UIImage imageNamed:@"7.jpg"];controller.printingItem = printImage;/************************* Print ************************//* Set up a completion handler block. If the print job has an error before spooling, this is where it's handled. */void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {if(completed && error)NSLog( @"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 );};if([UIDevice currentDevice].userInterfaceIdiom == UIUserInterfaceIdiomPad) {//iPad[controller presentFromRect:self.view.frame inView:self.view animated:YES completionHandler:completionHandler];}else{[controller presentAnimated:YES completionHandler:completionHandler];}}

iOS8以前實現的步驟和缺陷

這里主要步驟如下:
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);[pickerController presentFromRect:rect inView:self.view animated:YES completionHandler:^(UIPrinterPickerController *controller, BOOL userDidSelect, NSError *err){if (userDidSelect){// save the urlString and Printer name, do your UI interactionsself.printerName.text = controller.selectedPrinter.displayName;self.submitButton.enabled = YES;GSignInConfig.airPrinterUrlStr = controller.selectedPrinter.URL.absoluteString;GSignInConfig.airPrinterName = controller.selectedPrinter.displayName;DDLogInfo(@"Selected printer:%@", controller.selectedPrinter.displayName);}}];

UIPrinterPickerController需要iOS 8+;其設計理念是通過一次用戶交互將打印機相關信息從iOS系統傳遞到客戶端,客戶端后續可以用這些信息來進行打印。這里最關鍵的信息是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 {/************************* Set Print Info ************************/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;/************************* Print ************************//* Set up a completion handler block. If the print job has an error before spooling, this is where it's handled. */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而來,iOS8以前的版本也都可以使用。

參考鏈接:
airPrint蘋果官方參考資料

<完>

總結

以上是生活随笔為你收集整理的AirPrint: 无交互的后台打印实现(Print without UI,iOS8+)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。