From d2486217f0c8c6e9e60ce441cf1aded230412e36 Mon Sep 17 00:00:00 2001 From: kentongray Date: Tue, 5 Sep 2017 13:10:59 -0500 Subject: [PATCH] Cache UIPrinter (#128) * Cache UIPrinter This speeds up printing time from 25 seconds to < 5 * Switching to isEqualToString --- src/ios/APPPrinter.h | 3 +++ src/ios/APPPrinter.m | 11 +++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/ios/APPPrinter.h b/src/ios/APPPrinter.h index a2d1dd1..734ccd6 100755 --- a/src/ios/APPPrinter.h +++ b/src/ios/APPPrinter.h @@ -25,6 +25,9 @@ @interface APPPrinter : CDVPlugin +// this is used to cache the uiprinter making repeated prints faster +@property (nonatomic) UIPrinter *previousPrinter; + // Find out whether printing is supported on this platform - (void) check:(CDVInvokedUrlCommand*)command; // Displays system interface for selecting a printer diff --git a/src/ios/APPPrinter.m b/src/ios/APPPrinter.m index 798038b..bba5fce 100755 --- a/src/ios/APPPrinter.m +++ b/src/ios/APPPrinter.m @@ -195,8 +195,15 @@ printer:(NSString*)printerId { NSURL* url = [NSURL URLWithString:printerId]; - UIPrinter* printer = [UIPrinter printerWithURL:url]; - + + // check to see if we have previously created this printer to reduce printing/"contacting" time + if(self.previousPrinter == nil || ![[[self.previousPrinter URL] absoluteString] isEqualToString: printerId]) { + self.previousPrinter = [UIPrinter printerWithURL:url]; + } + + UIPrinter* printer = self.previousPrinter; + + [controller printToPrinter:printer completionHandler: ^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) { CDVPluginResult* pluginResult =