Fix some warnings/deprecations for iOS and improve code

This commit is contained in:
Sebastián Katzer 2018-12-21 12:47:34 +01:00
parent 6e44eb9cde
commit c19cec7415
3 changed files with 216 additions and 215 deletions

View File

@ -19,15 +19,10 @@
under the License. under the License.
*/ */
#import <Foundation/Foundation.h>
#import <Cordova/CDVPlugin.h> #import <Cordova/CDVPlugin.h>
@interface APPPrinter : CDVPlugin <UIWebViewDelegate, UIPrintInteractionControllerDelegate> @interface APPPrinter : CDVPlugin <UIWebViewDelegate, UIPrintInteractionControllerDelegate>
// this is used to cache the uiprinter making repeated prints faster
@property (nonatomic) UIPrinter *previousPrinter;
// Find out whether printing is supported on this platform // Find out whether printing is supported on this platform
- (void) check:(CDVInvokedUrlCommand*)command; - (void) check:(CDVInvokedUrlCommand*)command;
// Displays system interface for selecting a printer // Displays system interface for selecting a printer

View File

@ -26,6 +26,8 @@
@property (retain) NSString* callbackId; @property (retain) NSString* callbackId;
@property (retain) NSMutableDictionary* settings; @property (retain) NSMutableDictionary* settings;
// this is used to cache the uiprinter making repeated prints faster
@property (nonatomic) UIPrinter *previousPrinter;
@end @end
@ -37,9 +39,6 @@
/* /*
* Checks if the printing service is available. * Checks if the printing service is available.
*
* @param {Function} callback
* A callback function to be called with the result
*/ */
- (void) check:(CDVInvokedUrlCommand*)command - (void) check:(CDVInvokedUrlCommand*)command
{ {
@ -59,51 +58,39 @@
/** /**
* Sends the printing content to the printer controller and opens them. * Sends the printing content to the printer controller and opens them.
*
* @param {NSString} content
* The (HTML encoded) content
*/ */
- (void) print:(CDVInvokedUrlCommand*)command - (void) print:(CDVInvokedUrlCommand*)command
{ {
if (!self.isPrintingAvailable) { if (!self.isPrintingAvailable) return;
return;
}
_callbackId = command.callbackId;
NSArray* arguments = [command arguments];
NSString* content = [arguments objectAtIndex:0];
self.settings = [arguments objectAtIndex:1];
UIPrintInteractionController* controller = [self printController]; UIPrintInteractionController* controller = [self printController];
controller.delegate = self; NSString* content = command.arguments[0];
_settings = command.arguments[1];
_callbackId = command.callbackId;
[self adjustPrintController:controller
withSettings:_settings];
[self adjustPrintController:controller withSettings:self.settings];
[self loadContent:content intoPrintController:controller]; [self loadContent:content intoPrintController:controller];
} }
/** /**
* Displays system interface for selecting a printer * Displays system interface for selecting a printer.
*
* @param command
* Contains the callback function and picker options if applicable
*/ */
- (void) pick:(CDVInvokedUrlCommand*)command - (void) pick:(CDVInvokedUrlCommand*)command
{ {
if (!self.isPrintingAvailable) { if (!self.isPrintingAvailable) return;
return;
} NSMutableDictionary* settings = command.arguments[0];
NSArray* bounds = settings[@"bounds"];
_callbackId = command.callbackId; _callbackId = command.callbackId;
CGRect rect;
NSArray* arguments = [command arguments]; if (bounds) {
NSMutableDictionary* settings = [arguments objectAtIndex:0];
CGRect rect = CGRectMake(40, 30, 0, 0); //Default in documentation
if (settings != (id)[NSNull null] && [settings objectForKey:@"bounds"] != nil){
NSArray* bounds = [settings objectForKey:@"bounds"];
rect = [self convertIntoRect:bounds]; rect = [self convertIntoRect:bounds];
} else {
rect = CGRectMake(40, 30, 0, 0);
} }
[self presentPrinterPicker:rect]; [self presentPrinterPicker:rect];
@ -115,32 +102,80 @@
/** /**
* Sent after a web view finishes loading a frame. * Sent after a web view finishes loading a frame.
* *
* @param webView * @param webView The web view has finished loading.
* The web view has finished loading. *
* @return [ Void ]
*/ */
- (void) webViewDidFinishLoad:(UIWebView *)webView - (void) webViewDidFinishLoad:(UIWebView *)webView
{ {
UIPrintInteractionController* controller = [self printController]; UIPrintInteractionController* controller = [self printController];
NSString* printerId = [self.settings objectForKey:@"printerId"]; NSString* printerID = _settings[@"printerId"];
if (( ![printerId isEqual:[NSNull null]] ) && ( [printerId length] > 0 )) { if (![printerID isEqual:[NSNull null]] && printerID.length > 0)
[self sendToPrinter:controller printer:printerId]; {
[self sendToPrinter:controller printer:printerID];
return; return;
} }
NSArray* bounds = [self.settings objectForKey:@"bounds"]; NSArray* bounds = self.settings[@"bounds"];
CGRect rect = [self convertIntoRect:bounds]; CGRect rect = [self convertIntoRect:bounds];
[self presentPrintController:controller fromRect:rect]; [self presentPrintController:controller fromRect:rect];
} }
#pragma mark -
#pragma mark UIPrintInteractionControllerDelegate
/**
* Asks the delegate for an object encapsulating the paper size and printing
* area to use for the print job. If Paper-Size is given it selects the best
* fitting papersize
*/
- (UIPrintPaper *) printInteractionController:(UIPrintInteractionController *)printInteractionController
choosePaper:(NSArray *)paperList
{
double height = [self.settings[@"paperHeight"] doubleValue];
double width = [self.settings[@"paperWidth"] doubleValue];
UIPrintPaper* paper;
if (height && width)
{
double dotsHeigth = 72 * height / 25.4; //convert milimeters to dots
double dotsWidth = 72 * width / 25.4; //convert milimeters to dots
CGSize pageSize = CGSizeMake(dotsHeigth, dotsWidth);
// get best fitting paper size
paper = [UIPrintPaper bestPaperForPageSize:pageSize
withPapersFromArray:paperList];
}
return paper;
}
/**
* Asks the delegate for a length to use when cutting the page. If using roll
* printers like Label-Printer (brother QL-710W) you can cut paper after given
* length.
*/
- (CGFloat) printInteractionController:(UIPrintInteractionController *)printInteractionController
cutLengthForPaper:(UIPrintPaper *)paper
{
double length = [self.settings[@"paperCutLength"] doubleValue];
CGFloat height = paper.paperSize.height;
if (length)
{
height = 72 * length / 25.4; //convert milimeters to dots
}
return height;
}
#pragma mark - #pragma mark -
#pragma mark Core #pragma mark Core
/** /**
* Checks either the printing service is avaible or not. * Checks either the printing service is avaible or not.
*
* @return {BOOL}
*/ */
- (BOOL) isPrintingAvailable - (BOOL) isPrintingAvailable
{ {
@ -154,125 +189,120 @@
isPrintingAvailable]; isPrintingAvailable];
} }
/**
* Sends the content directly to the specified or previously selected printer.
*
* @param controller The prepared print controller with the content.
* @param printer The printer specified by its URL.
*
* @return [ Void ]
*/
- (void) sendToPrinter:(UIPrintInteractionController*)controller
printer:(NSString*)printerID
{
NSURL* printerURL = [NSURL URLWithString:printerID];
// check to see if we have previously created this printer to reduce printing/"contacting" time
if (!_previousPrinter || ![_previousPrinter.URL.absoluteString isEqualToString:printerID])
{
_previousPrinter = [UIPrinter printerWithURL:printerURL];
}
[controller printToPrinter:_previousPrinter completionHandler:
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
CDVPluginResult* result =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:ok];
[self.commandDelegate sendPluginResult:result
callbackId:self->_callbackId];
}];
}
/** /**
* Opens the print controller so that the user can choose between * Opens the print controller so that the user can choose between
* available iPrinters. * available iPrinters.
* *
* @param {UIPrintInteractionController} controller * @param controller The prepared print preview controller.
* The prepared print controller with a content * @param rect The coordinates where to present the preview.
*
* @return [ Void ]
*/ */
- (void) presentPrintController:(UIPrintInteractionController*)controller - (void) presentPrintController:(UIPrintInteractionController*)controller
fromRect:(CGRect)rect fromRect:(CGRect)rect
{ {
UIPrintInteractionCompletionHandler handler =
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
CDVPluginResult* result =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:ok];
[self.commandDelegate sendPluginResult:result
callbackId:self.callbackId];
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromRect:rect inView:self.webView animated:YES completionHandler: [controller presentFromRect:rect
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) { inView:self.webView
CDVPluginResult* pluginResult = animated:YES
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK completionHandler:handler];
messageAsBool:ok]; } else {
[controller presentAnimated:YES
[self.commandDelegate sendPluginResult:pluginResult completionHandler:handler];
callbackId:_callbackId];
}];
}
else {
[controller presentAnimated:YES completionHandler:
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
CDVPluginResult* pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:ok];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}];
} }
} }
/** /**
* Sends the content directly to the specified printer. * Displays system interface for selecting a printer.
* *
* @param controller * @param rect Defines where to display the interface on the screen.
* The prepared print controller with the content
* @param printer
* The printer specified by its URL
*/
- (void) sendToPrinter:(UIPrintInteractionController*)controller
printer:(NSString*)printerId
{
NSURL* url = [NSURL URLWithString:printerId];
// 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 =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:ok];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}];
}
/**
* Displays system interface for selecting a printer
* *
* @param rect * @return [ Void ]
* Rect object of where to display the interface
*/ */
- (void) presentPrinterPicker:(CGRect)rect - (void) presentPrinterPicker:(CGRect)rect
{ {
UIPrinterPickerController* controller = UIPrinterPickerController* controller =
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil]; [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
UIPrinterPickerCompletionHandler handler =
^(UIPrinterPickerController *ctrl, BOOL selected, NSError *e) {
[self returnPrinterPickerResult:ctrl];
};
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromRect:rect inView:self.webView animated:YES completionHandler: [controller presentFromRect:rect
^(UIPrinterPickerController *ctrl, BOOL userDidSelect, NSError *e) { inView:self.webView
[self returnPrinterPickerResult:ctrl animated:YES
withUserDidSelect:&userDidSelect]; completionHandler:handler];
}]; } else {
} [controller presentAnimated:YES
else { completionHandler:handler];
[controller presentAnimated:YES completionHandler:
^(UIPrinterPickerController *ctrl, BOOL userDidSelect, NSError *e) {
[self returnPrinterPickerResult:ctrl
withUserDidSelect:&userDidSelect];
}];
} }
} }
/** /**
* Calls the callback funtion with the result of the selected printer * Calls the callback funtion with the result of the selected printer.
* *
* @param ctrl * @param controller The UIPrinterPickerController used to display
* The UIPrinterPickerController used to display the printer selector interface * the printer selector interface.
* @param userDidSelect *
* True if the user selected a printer * @return [ Void ]
*/ */
- (void) returnPrinterPickerResult:(UIPrinterPickerController*)ctrl - (void) returnPrinterPickerResult:(UIPrinterPickerController*)controller
withUserDidSelect:(BOOL*)userDidSelect
{ {
CDVPluginResult* pluginResult = CDVPluginResult* result;
[CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT]; UIPrinter* printer = controller.selectedPrinter;
if (userDidSelect) { [self rememberPrinter:printer];
UIPrinter* printer = ctrl.selectedPrinter;
[UIPrinterPickerController if (printer) {
printerPickerControllerWithInitiallySelectedPrinter:printer]; result = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
pluginResult = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsString:printer.URL.absoluteString]; messageAsString:printer.URL.absoluteString];
} else {
result = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
} }
[self.commandDelegate sendPluginResult:pluginResult [self.commandDelegate sendPluginResult:result
callbackId:_callbackId]; callbackId:_callbackId];
} }
@ -280,23 +310,25 @@
#pragma mark Helper #pragma mark Helper
/** /**
* Retrieves an instance of shared print controller. * Returns the shared instance of the printer controller.
*
* @return {UIPrintInteractionController*}
*/ */
- (UIPrintInteractionController*) printController - (UIPrintInteractionController*) printController
{ {
return [UIPrintInteractionController sharedPrintController]; UIPrintInteractionController* controller = [UIPrintInteractionController
sharedPrintController];
controller.delegate = self;
return controller;
} }
/** /**
* Adjusts the settings for the print controller. * Adjusts the settings for the print controller.
* *
* @param {UIPrintInteractionController} controller * @param controller The print controller instance.
* The print controller instance * @param settings The print job specs.
* *
* @return {UIPrintInteractionController} controller * @return The modified print controller instance
* The modified print controller instance
*/ */
- (UIPrintInteractionController*) adjustPrintController:(UIPrintInteractionController*)controller - (UIPrintInteractionController*) adjustPrintController:(UIPrintInteractionController*)controller
withSettings:(NSMutableDictionary*)settings withSettings:(NSMutableDictionary*)settings
@ -306,132 +338,107 @@
UIPrintInfoOutputType outputType = UIPrintInfoOutputGeneral; UIPrintInfoOutputType outputType = UIPrintInfoOutputGeneral;
UIPrintInfoDuplex duplexMode = UIPrintInfoDuplexNone; UIPrintInfoDuplex duplexMode = UIPrintInfoDuplexNone;
if ([[settings objectForKey:@"landscape"] boolValue]) { if ([settings[@"landscape"] boolValue]) {
orientation = UIPrintInfoOrientationLandscape; orientation = UIPrintInfoOrientationLandscape;
} }
if ([[settings objectForKey:@"graystyle"] boolValue]) { if ([settings[@"graystyle"] boolValue]) {
outputType = UIPrintInfoOutputGrayscale; outputType = UIPrintInfoOutputGrayscale;
} }
outputType += [[settings objectForKey:@"border"] boolValue] ? 0 : 1; outputType += [settings[@"border"] boolValue] ? 0 : 1;
if ([[settings objectForKey:@"duplex"] isEqualToString:@"long"]) { if ([settings[@"duplex"] isEqualToString:@"long"]) {
duplexMode = UIPrintInfoDuplexLongEdge; duplexMode = UIPrintInfoDuplexLongEdge;
} else } else
if ([[settings objectForKey:@"duplex"] isEqualToString:@"short"]) { if ([settings[@"duplex"] isEqualToString:@"short"]) {
duplexMode = UIPrintInfoDuplexShortEdge; duplexMode = UIPrintInfoDuplexShortEdge;
} }
printInfo.outputType = outputType; printInfo.outputType = outputType;
printInfo.orientation = orientation; printInfo.orientation = orientation;
printInfo.duplex = duplexMode; printInfo.duplex = duplexMode;
printInfo.jobName = [settings objectForKey:@"name"]; printInfo.jobName = settings[@"name"];
controller.printInfo = printInfo; controller.printInfo = printInfo;
controller.showsPageRange = ![[settings objectForKey:@"hidePageRange"] boolValue]; controller.showsNumberOfCopies = ![settings[@"hideNumberOfCopies"] boolValue];
controller.showsNumberOfCopies = ![[settings objectForKey:@"hideNumberOfCopies"] boolValue]; controller.showsPaperSelectionForLoadedPapers = ![settings[@"hidePaperFormat"] boolValue];
controller.showsPaperSelectionForLoadedPapers = ![[settings objectForKey:@"hidePaperFormat"] boolValue];
return controller; return controller;
} }
/**
* Choose paper Delegate. If Paper-Size is given it selects the best fitting papersize
*/
- (UIPrintPaper *) printInteractionController:(UIPrintInteractionController *)printInteractionController choosePaper:(NSArray *)paperList {
if ([[self.settings objectForKey:@"paperHeight"] doubleValue] && [[self.settings objectForKey:@"paperHeight"] doubleValue]){
double heigth = [[self.settings objectForKey:@"paperHeight"] doubleValue];
double width = [[self.settings objectForKey:@"paperWidth"] doubleValue];
double dotsHeigth = 72*heigth / 25.4; //convert milimeters to dots
double dotsWidth = 72*width / 25.4; //convert milimeters to dots
CGSize pageSize = CGSizeMake(dotsHeigth, dotsWidth);
// get best fitting paper size
UIPrintPaper* paper = [UIPrintPaper bestPaperForPageSize:pageSize withPapersFromArray:paperList];
return paper;
}
else {
return NULL;
}
}
/**
* cutPaper Delegate. If using roll printers like Label-Printer (brother QL-710W) you can cut paper after given length.
*/
- (CGFloat)printInteractionController:(UIPrintInteractionController *)printInteractionController
cutLengthForPaper:(UIPrintPaper *)paper {
if ([[self.settings objectForKey:@"paperCutLength"] doubleValue]){
double cutLength = [[self.settings objectForKey:@"paperCutLength"] doubleValue];
return 72 * cutLength / 25.4; //convert milimeters to dots
} else {
return paper.paperSize.height;
}
}
/** /**
* Loads the content into the print controller. * Loads the content into the print controller.
* *
* @param {NSString} content * @param content The (HTML encoded) content
* The (HTML encoded) content * @param controller The print controller instance
* @param {UIPrintInteractionController} controller *
* The print controller instance * @return [ Void ]
*/ */
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller - (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
{ {
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init]; UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
UIViewPrintFormatter* formatter; UIViewPrintFormatter* formatter = self.webView.viewPrintFormatter;
bool printSelf = ![content isEqual:[NSNull null]] && content.length == 0;
if([content length] == 0) { if (!printSelf)
formatter = [self.webView viewPrintFormatter]; {
} else {
UIWebView* page = [[UIWebView alloc] init]; UIWebView* page = [[UIWebView alloc] init];
formatter = [page viewPrintFormatter]; formatter = page.viewPrintFormatter;
page.delegate = self; page.delegate = self;
if([content length] == 0) { if ([NSURL URLWithString:content])
// do nothing, already loaded {
} else if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content]; NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]]; [page loadRequest:[NSURLRequest requestWithURL:url]];
} else { }
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www" else
ofType:nil]; {
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath]; NSString* path = [NSBundle.mainBundle pathForResource:@"www" ofType:nil];
NSURL *url = [NSURL fileURLWithPath:path];
[page loadHTMLString:content baseURL:url];
[page loadHTMLString:content baseURL:baseURL];
} }
} }
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0]; [renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
controller.printPageRenderer = renderer; [controller setPrintPageRenderer:renderer];
if (printSelf) {
// just trigger the finish load fn straight off if using current webView // just trigger the finish load fn straight off if using current webView
if([content length] == 0)
[self webViewDidFinishLoad:(UIWebView *)self.webView]; [self webViewDidFinishLoad:(UIWebView *)self.webView];
}
}
/**
* Tells the system to pre-select the given printer next time.
*
* @param printer The printer to remeber.
*
* @return [ Void ]
*/
- (void) rememberPrinter:(UIPrinter*)printer
{
if (!printer) return;
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printer];
} }
/** /**
* Convert Array into Rect object. * Convert Array into Rect object.
* *
* @param bounds * @param bounds The bounds
* The bounds
* *
* @return * @return A converted Rect object
* A converted Rect object
*/ */
- (CGRect) convertIntoRect:(NSArray*)bounds - (CGRect) convertIntoRect:(NSArray*)bounds
{ {
return CGRectMake([[bounds objectAtIndex:0] floatValue], return CGRectMake([bounds[0] floatValue],
[[bounds objectAtIndex:1] floatValue], [bounds[1] floatValue],
[[bounds objectAtIndex:2] floatValue], [bounds[2] floatValue],
[[bounds objectAtIndex:3] floatValue]); [bounds[3] floatValue]);
} }
@end @end

View File

@ -35,7 +35,6 @@ exports.getDefaults = function () {
graystyle: false, graystyle: false,
// iOS specific // iOS specific
border: true, border: true,
hidePageRange: false,
hideNumberOfCopies: false, hideNumberOfCopies: false,
hidePaperFormat: false, hidePaperFormat: false,
// iPad specific // iPad specific