iOS打印功能整理

最近有個打印App內網頁的需求,在網上找了一些資料,實現了相關功能,順便寫出來,方便查找


/// 開始打印
- (void)startPrint{
    
    NSDictionary *latestPrinterInfo = [Utils userDefaultsValueForKey:@"TCM_PrinterInfo"];
    
    UIPrinter *printer = nil;
    
    if (!TCM_ValidDict(latestPrinterInfo)) {
        //找不到打印機信息,則選擇打印機
        [self pickPrinter];
        return;
    }else{
        
        NSString *url = [latestPrinterInfo objectForKey:@"url"];
//        NSString *name = [latestPrinterInfo objectForKey:@"name"];
        
        if (!TCM_ValidString(url)) {
            //打印機信息無效,則選擇打印機
            [self pickPrinter];
            return;
        }
        
        printer = [UIPrinter printerWithURL:[NSURL URLWithString:url]];
        
        if (!printer) {
            //初始化打印機失敗,則選擇打印機
            [self pickPrinter];
            return;
        }
    }
    
    [self printWebpageWithPrinter:printer];
}

/// 選擇打印機
- (void)pickPrinter{
    
    UIPrinterPickerController *pickerController = [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
    
    UIPrinterPickerCompletionHandler completionHandler = ^(UIPrinterPickerController * _Nonnull printerPickerController, BOOL userDidSelect, NSError * _Nullable error) {
        
        if (userDidSelect) {
            
            NSDictionary *latestPrinterInfo = @{
                @"url": printerPickerController.selectedPrinter.URL.absoluteString,
                @"name": printerPickerController.selectedPrinter.displayName
            };
            
            [Utils setUserDefaultsValue:latestPrinterInfo forKey:@"TCM_PrinterInfo"];
            
            DLog(@"Printer Info: %@", latestPrinterInfo.debugDescription);
            
            [self printWebpageWithPrinter:printerPickerController.selectedPrinter];
        }
    };
    
    if (DEVICE_IS_Pad) {
        [pickerController presentFromRect:CGRectZero inView:self.view animated:YES completionHandler:completionHandler];
    }else{
        [pickerController presentAnimated:YES completionHandler:completionHandler];
    }
}

/// 打印網頁
/// @param printer 打印機
- (void)printWebpageWithPrinter:(UIPrinter *)printer{
    
    if (!printer) {
        
        return;
    }
    
    //連接打印機
    [printer contactPrinter:^(BOOL available) {
        
        if (available) {
            
            UIPrintInteractionController *printController = [UIPrintInteractionController sharedPrintController];
            
            if (!printController) {
                
                [TCMProgressHUDInstance showMessage:@"打印功能異常" dismissAfterDelay:2.5];
                return;
            }
            
            //回調
            UIPrintInteractionCompletionHandler completionHandler = ^(UIPrintInteractionController * _Nonnull printInteractionController, BOOL completed, NSError * _Nullable error) {
                
                if (completed) {
                    [TCMProgressHUDInstance showMessage:@"開始打印" dismissAfterDelay:2.5];
                }else{
                    
                    if (error) {
                        [TCMProgressHUDInstance showMessage:error.debugDescription dismissAfterDelay:2.5];
                    }else{
                        [TCMProgressHUDInstance showMessage:@"打印取消" dismissAfterDelay:2.5];
                    }
                    DLog(@"打印結果:%@", error.debugDescription);
                }
            };
            
            UIPrintInfo *printInfo = [UIPrintInfo printInfo];
            printInfo.outputType = UIPrintInfoOutputGeneral;
            printInfo.jobName = @"打印網頁";
            printInfo.duplex = UIPrintInfoDuplexLongEdge;
            
            printController.printInfo = printInfo;
            printController.showsPageRange = YES;
//            printController.showsNumberOfCopies = NO;
            
            UIViewPrintFormatter *printFormatter = [self.commonWebView viewPrintFormatter];
            printFormatter.startPage = 0;
            printController.printFormatter = printFormatter;
            
            if (DEVICE_IS_Pad) {
                [printController presentFromRect:CGRectZero inView:self.view animated:YES completionHandler:completionHandler];
            }else{
                [printController presentAnimated:YES completionHandler:completionHandler];
            }
        }else{
            
            [self pickPrinter];
        }
    }];
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章