Committed by
GitHub
fix: re-init UIPrinter cause issues with delegate (#1363)
* fix: re-init UIPrinter cause issues with delegate * change selectedPrinters to map of URL:UIPrinter
Showing
1 changed file
with
27 additions
and
5 deletions
| @@ -21,6 +21,10 @@ func dataProviderReleaseDataCallback(info _: UnsafeMutableRawPointer?, data: Uns | @@ -21,6 +21,10 @@ func dataProviderReleaseDataCallback(info _: UnsafeMutableRawPointer?, data: Uns | ||
| 21 | data.deallocate() | 21 | data.deallocate() |
| 22 | } | 22 | } |
| 23 | 23 | ||
| 24 | +// A variable that holds the selected printers to prevent recreate it if selected again | ||
| 25 | +// Each printer will be identified by its URL string | ||
| 26 | +var selectedPrinters = [String: UIPrinter]() | ||
| 27 | + | ||
| 24 | public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate { | 28 | public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate { |
| 25 | private var printing: PrintingPlugin | 29 | private var printing: PrintingPlugin |
| 26 | public var index: Int | 30 | public var index: Int |
| @@ -93,14 +97,19 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -93,14 +97,19 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 93 | return | 97 | return |
| 94 | } | 98 | } |
| 95 | 99 | ||
| 96 | - let printer = UIPrinter(url: printerURL!) | ||
| 97 | - printer.contactPrinter { available in | 100 | + let printerURLString = printerURL!.absoluteString |
| 101 | + | ||
| 102 | + if !selectedPrinters.keys.contains(printerURLString) { | ||
| 103 | + selectedPrinters[printerURLString] = UIPrinter(url: printerURL!) | ||
| 104 | + } | ||
| 105 | + | ||
| 106 | + selectedPrinters[printerURLString]!.contactPrinter { available in | ||
| 98 | if !available { | 107 | if !available { |
| 99 | self.printing.onCompleted(printJob: self, completed: false, error: "Printer not available") | 108 | self.printing.onCompleted(printJob: self, completed: false, error: "Printer not available") |
| 100 | return | 109 | return |
| 101 | } | 110 | } |
| 102 | 111 | ||
| 103 | - controller.print(to: printer, completionHandler: self.completionHandler) | 112 | + controller.print(to: selectedPrinters[printerURLString]!, completionHandler: self.completionHandler) |
| 104 | } | 113 | } |
| 105 | } else { | 114 | } else { |
| 106 | controller.present(animated: true, completionHandler: self.completionHandler) | 115 | controller.present(animated: true, completionHandler: self.completionHandler) |
| @@ -184,8 +193,21 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | @@ -184,8 +193,21 @@ public class PrintJob: UIPrintPageRenderer, UIPrintInteractionControllerDelegate | ||
| 184 | return | 193 | return |
| 185 | } | 194 | } |
| 186 | 195 | ||
| 187 | - let printer = UIPrinter(url: printerURL!) | ||
| 188 | - controller.print(to: printer, completionHandler: completionHandler) | 196 | + let printerURLString = printerURL!.absoluteString |
| 197 | + | ||
| 198 | + if !selectedPrinters.keys.contains(printerURLString) { | ||
| 199 | + selectedPrinters[printerURLString] = UIPrinter(url: printerURL!) | ||
| 200 | + } | ||
| 201 | + | ||
| 202 | + | ||
| 203 | + selectedPrinters[printerURLString]!.contactPrinter { available in | ||
| 204 | + if !available { | ||
| 205 | + self.printing.onCompleted(printJob: self, completed: false, error: "Printer not available") | ||
| 206 | + return | ||
| 207 | + } | ||
| 208 | + | ||
| 209 | + controller.print(to: selectedPrinters[printerURLString]!, completionHandler: self.completionHandler) | ||
| 210 | + } | ||
| 189 | return | 211 | return |
| 190 | } | 212 | } |
| 191 | 213 |
-
Please register or login to post a comment