沃梦达 / IT编程 / 移动开发 / 正文

使用IOS AirPrint实现打印功能详解

这篇文章主要介绍了使用IOS AirPrint实现打印功能详解,想了解无线打印的同学,一定要看一下

内容

1.什么是AirPrint

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

2.第一手资料

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

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

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


- (IBAction)printContent:(id)sender {

    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];

    if (pic && [UIPrintInteractionController canPrintData: self.myPDFData] ) {

    pic.delegate = self;

     
    UIPrintInfo *printInfo = [UIPrintInfo printInfo];

    printInfo.outputType = UIPrintInfoOutputGeneral;

    printInfo.jobName = [self.path lastPathComponent];

    printInfo.duplex = UIPrintInfoDuplexLongEdge;

    pic.printInfo = printInfo;

    pic.showsPageRange = YES;

    pic.printingItem = self.myPDFData;

     
    void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =

    ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {

    self.content = nil;

    if (!completed && error)

    NSLog(@"FAILED! due to error in domain %@ with error code %u",

    error.domain, error.code);

    };

    if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {

    [pic presentFromBarButtonItem:self.printButton animated:YES

    completionHandler:completionHandler];

    } else {

    [pic presentAnimated:YES completionHandler:completionHandler];

    }

}

通过在iPhone上测试, 显示出的全部是英文的,不要担心, 因为这是系统的控件,也就是说系统会自动帮你作国际化处理,你不用作任何事情!
你唯一要作的事–––将Info.plist文件中的第一项 Localization native development region(CFBundleDevelopmentRegion)的值设为 China(zh_CN);

UISimpleTextPrintFormatter—automatically draws and lays out plain-text documents. This formatter allows you to set global properties for the text, such a font, color, alignment, and line-break mode.

  • UIMarkupTextPrintFormatter—automatically draws and lays out HTML documents.
  • 英文介绍已经很详细了, 就不啰嗦了, 直接展示出打印HTML文档的代码:

    
    - (IBAction)printContent:(id)sender {
    
        UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    
        pic.delegate = self;
    
         
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    
        printInfo.outputType = UIPrintInfoOutputGeneral;
    
        printInfo.jobName = self.documentName;
    
        pic.printInfo = printInfo;
    
         
        UIMarkupTextPrintFormatter *htmlFormatter = [[UIMarkupTextPrintFormatter alloc]
    
        initWithMarkupText:self.htmlString];
    
        htmlFormatter.startPage = 0;
    
        htmlFormatter.contentInsets = UIEdgeInsetsMake(72.0, 72.0, 72.0, 72.0); // 1 inch margins
    
        pic.printFormatter = htmlFormatter;
    
        pic.showsPageRange = YES;
    
         
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    
        ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    
        if (!completed && error) {
    
        NSLog(@"Printing could not complete because of error: %@", error);
    
        }
    
        };
    
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    
        [pic presentFromBarButtonItem:sender animated:YES completionHandler:completionHandler];
    
        } else {
    
        [pic presentAnimated:YES completionHandler:completionHandler];
    
        }
    
    }

    将UIWebView 界面上显示的内容打印出来。

    
    - (void)printWebPage:(id)sender {
    
        UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
    
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
    
        ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
    
        if(!completed && error){
    
        NSLog(@"FAILED! due to error in domain %@ with error code %u",
    
        error.domain, error.code);
    
        }
    
        };
    
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
    
        printInfo.outputType = UIPrintInfoOutputGeneral;
    
        printInfo.jobName = [urlField text];
    
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
    
        controller.printInfo = printInfo;
    
        controller.showsPageRange = YES;
    
         
        UIViewPrintFormatter *viewFormatter = [self.myWebView viewPrintFormatter];
    
        viewFormatter.startPage = 0;
    
        controller.printFormatter = viewFormatter;
    
         
        if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    
        [controller presentFromBarButtonItem:printButton animated:YES completionHandler:completionHandler];
    
        }else
    
        [controller presentAnimated:YES completionHandler:completionHandler];
    
    }
    

    9.Using a Page Renderer(页渲染器)

    这部分内容是最复杂的了, 感觉不怎么用,暂且不深究了, 大家如果项目需要, 自己看文档吧。

    以上就是使用IOS AirPrint实现打印功能详解的详细内容,更多关于IOS AirPrint打印功能的资料请关注编程学习网其它相关文章!

    本文标题为:使用IOS AirPrint实现打印功能详解