问题
I have the following codes to print a simple text. I'm using HP Officejet 4630 series. But I'm getting UIPrintErrorDomain error 4 as well as error 3.
Any idea why?
UIPrintInfo *pi = [UIPrintInfo printInfo];
pi.outputType = UIPrintInfoOutputGeneral;
pi.jobName = @"test";
UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
pic.printInfo = pi;
UISimpleTextPrintFormatter *formatter = [[UISimpleTextPrintFormatter alloc] initWithText:@"testing 123"];
formatter.contentInsets = UIEdgeInsetsMake(72, 72, 72, 72);
pic.printFormatter = formatter;
NSString *url = [[NSUserDefaults standardUserDefaults] objectForKey:@"printer"];
UIPrinter *printer = [UIPrinter printerWithURL:[NSURL URLWithString:url]];
[pic printToPrinter:printer completionHandler:^(UIPrintInteractionController * __nonnull printInteractionController, BOOL completed, NSError * __nullable error) {
NSLog(@"error -> %@", error);
}];
I have correct printer URL. I'm getting the following error.
ipp://HPD0...15287.local.:631/ipp/print: startJob: Unable to connect to printd: Bad file descriptor
Error Domain=UIPrintErrorDomain Code=4 "The operation couldn’t be completed. (UIPrintErrorDomain error 4.)"
回答1:
Also lost some time seeking for solution. Found such one:
- First - choose printer for the first time (Just as it was before ios 13) and save its printer NSURL.
private selectPrinter() {
const printPicker = UIPrinterPickerController.printerPickerControllerWithInitiallySelectedPrinter(null);
const view = utils.ios
.getter(UIApplication, UIApplication.sharedApplication).keyWindow.rootViewController.view;
const theFrame = frame.topmost().currentPage.frame;
printPicker.presentFromRectInViewAnimatedCompletionHandler(
theFrame, view, true, (printerPicker, userDidSelect, error) => {
if (userDidSelect) {
this.someService.printerUrl = printerPicker.selectedPrinter.URL;
} else {
setTimeout(() => { this.selectPrinter(); }, 4);
}
},
);
}
- Then use printer by its NSURL without a necessity to choose printer each time Special thanks to @moeseth for idea.
const printer = UIPrinter.printerWithURL(this.someService.printerUrl);
printer.contactPrinter((available) => {
if (available) {
controller.printToPrinterCompletionHandler(printer, callback);
} else {
alert("Printer Not Found");
}
});
Hope it will help someone
来源:https://stackoverflow.com/questions/31289969/uiprinterrordomain-error-4-on-printing-with-uisimpletextprintformatter