iOS打印 AirPrint

使用iOS AirPrint 让你的APP轻松实现打印功能

1, 什么是AirPrint

其实就是将iOS(iphone,ipad)上的内容,使用支持AirPrint的打印机打印出来。打印过程无线控制, 非常方便。

2, 第一手资料

学习iOS, 第一手资料肯定非苹果官方文档莫属.here。 (我下面叙述的内容基本上是对文档的总结, 英语可以的建议直接看文档。。。)

3, Printer Simulator,使用打印模拟器进行测试

既然涉及打印功能,那么就需要有一台支持AirPrint 功能的打印机进行测试喽,你没有?没关系!苹果已经为我们准备好了模拟器。 这个模拟器在Xcode中没有, 需要自己到官网下载:下载Printer Simulator(需要先注册登录)



点击Printer Simulator即打开模拟打印机


4、打印代码

协议UIPrintInteractionControllerDelegate

主要是用到了UIPrintInteractionController类,这是一个单列类。UIPrintInfo是配置打印信息,UISimpleTextPrintFormatter是设置页面范围。

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)的值设为 China(zh_CN);



选择后打印会生成一张图片模拟结束
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 使用iOS AirPrint 让你的APP轻松实现打印功能 2016/05/13 · iOS开发 · 打印分享到:...
    萧城x阅读 11,752评论 2 10
  • 无线打印打印流程 a.创建 UIPrintInteractionController 实例。 b.创建UIPrin...
    huaiOS阅读 6,141评论 0 1
  • 本文转载xianghuibeijingCSDN 转载过程中部分内可能丢失!请参考原作者:http://blog....
    大熊Q阅读 8,204评论 0 12
  • 我们需要进度条,我们需要“剩余时间”。 想到之前火过的一个知乎问题:“为什么有些男生追到一半就不追了”,底下有一个...
    Kernholz阅读 1,111评论 0 0
  • 岁月如梭,匆匆而过。逝去的岁月,我们还能找回来吗?人生就像是一条单行线,有去无回。如果今天我们注重保养,...
    健康顾问一小欣阅读 10,299评论 0 0