Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
printer example fix in ios15
WKWebView * webView = [[WKWebView alloc] initWithFrame:CGRectZero];
[webView loadHTMLString:markup baseURL:nil]; // assuming some html loaded into markup for the printer
UIViewPrintFormatter *formatter = webView.viewPrintFormatter;
UIPrintInteractionController *printInteractionController = [UIPrintInteractionController sharedPrintController];
[printInteractionController setPrintFormatter:formatter];
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
printInfo.duplex = UIPrintInfoDuplexNone;
printInfo.outputType = UIPrintInfoOutputPhotoGrayscale;
self.printer = [UIPrinter printerWithURL:url]; // assuming you already have the printer url
[printInteractionController setDelegate:self];
[printInteractionController setPrintInfo:printInfo];
[self.printer contactPrinter:^(BOOL available) {
if (!available) {
[self printFailed]; // private handler
} else {
[printInteractionController printToPrinter:self.printer
completionHandler:^(UIPrintInteractionController *controller, BOOL completed, NSError *error) {
if ( error ) {
NSLog(@"Airprint error: %@", error);
[self printFailed]; // private handler
} else {
// success case here
}
controller.delegate = nil;
}];
}
}];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment