iOS开发iOS日常知识储备

iOS 后台无UI交互打印

2016-12-01  本文已影响109人  武小寺

iOS airPrint有交互打印
http://www.jianshu.com/p/f5863a1833d0

需求无交互打印(实现静默打印)
1.与有交互打印的唯一区别,就是不弹出让用户点print的界面

交互界面.png

2.选择打印机的页面是必不可少的(至少进行一次)

选择打印机

3.将用户选择的打印机信息,存储到本地,下次直接调用

//选择打印机
- (void)selectPrinterPicker{
    UIPrinterPickerController *pickerController =[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
    
    CGRect rect;
   
    rect = CGRectMake(0 , 0, 0, 0);
    [pickerController presentFromRect:rect inView:self.view animated:YES completionHandler:^(UIPrinterPickerController *controller, BOOL userDidSelect, NSError *err){
        if (userDidSelect)
        {
            //建议本地化存储,下次直接打印
            [self.dic setValue:controller.selectedPrinter.URL.absoluteString forKey:@"printUrl"];
            [self.dic setValue:controller.selectedPrinter.displayName forKey:@"printName"];

            NSLog(@"Selected printer:%@", controller.selectedPrinter.displayName);
        }
    }];
}

4.最重要的步骤就是打印了,打印前需要判断,之前存储的打印机是否还可以链接,可以连接直接打印,没有的话提示

//网页
- (void)printWebPage:(id)sender
{
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"printUrl"]);
//    UIPrinter *airPrint = [UIPrinter printerWithURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"printUrl"]]];
    
    NSLog(@"%@", self.dic);
    
    UIPrinter *airPrintDic = [UIPrinter printerWithURL:[NSURL URLWithString:[self.dic objectForKey:@"printUrl"]]];
    
    
    if (nil == airPrintDic.URL) {
        NSLog(@"暂无打印机");
        [self selectPrinterPicker];
        return;
    }
    
    [airPrintDic contactPrinter:^(BOOL available)
     {
         if (available)
         {
             NSLog(@"AIRPRINTER AVAILABLE");
             UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
             if(!controller){
                 NSLog(@"Couldn't get shared UIPrintInteractionController!");
                 return;
             }
             
             UIPrintInteractionCompletionHandler completionHandler =
             ^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
                 if(!completed && error){
                     NSLog(@"FAILED! due to error in domain %@ with error code %ld", error.domain, (long)error.code);
                 }
             };
             
             
             // Obtain a printInfo so that we can set our printing defaults.
             UIPrintInfo *printInfo = [UIPrintInfo printInfo];
             // This application produces General content that contains color.
             printInfo.outputType = UIPrintInfoOutputGrayscale;
             // We'll use the URL as the job name.
             printInfo.jobName = @"";
             // Set duplex so that it is available if the printer supports it. We are
             // performing portrait printing so we want to duplex along the long edge.
             printInfo.duplex = UIPrintInfoDuplexLongEdge;
             // Use this printInfo for this print job.
             controller.printInfo = printInfo;
             
             // Be sure the page range controls are present for documents of > 1 page.
             controller.showsPageRange = YES;
             
             //是否打印多份
             //    controller.showsNumberOfCopies = NO;
             
             // This code uses a custom UIPrintPageRenderer so that it can draw a header and footer.
             APLPrintPageRenderer *myRenderer = [[APLPrintPageRenderer alloc] init];
             myRenderer.jobTitle = printInfo.jobName;
             UIViewPrintFormatter *viewFormatter = [self.myWebView viewPrintFormatter];
             
             [myRenderer addPrintFormatter:viewFormatter startingAtPageAtIndex:0];
             // Set our custom renderer as the printPageRenderer for the print job.
             controller.printPageRenderer = myRenderer;
             
             [controller printToPrinter:airPrintDic completionHandler:completionHandler];
         }
         else
         {
             NSLog(@"AIRPRINTER NOT AVAILABLE");
             [self selectPrinterPicker];
         }
     }];
}

5.使用时,直接根据自己的业务,导入这俩个方法,调用打印时,调用4的方法即可(实现的是打印网页里面的内容)!

上一篇下一篇

猜你喜欢

热点阅读