diff --git a/README.md b/README.md index 9521d76..d83ad83 100644 --- a/README.md +++ b/README.md @@ -118,6 +118,7 @@ The plugin creates the object `cordova.plugins.printer` with the following metho 1. [printer.isAvailable][available] 2. [printer.print][print] +3. [printer.print][printerPicker] (iOS only) ### Plugin initialization The plugin and its methods are not available before the *deviceready* event has been fired. @@ -247,6 +248,40 @@ cordova.plugins.printer.print('123', { bounds:[40, 30, 0, 0] }); cordova.plugins.printer.print('123', { bounds:{ left:40, top:30, width:0 height:0 } }); ``` +### Display printer picker (iOS 8.0+ only) +Displays a system interface allowing the user to select an available printer. The callback function will return the network URL of the selected printer (null if no printer selected). The URL can be passed into the print function (printerId option), allowing a page to be printed without prompting the user. This feature is only available in iOS 8.0 and later. + +#### Available Options +| Name | Description | Type | Support | +| ---- | ----------- |:----:| -------:| +| bounds | The Size and position of the printer picker view | Array | iPad | + +#### Examples + +##### 1. Display the picker +```javascript +cordova.plugins.printer.printerPicker(function (printerId) { + alert(printerId) +}); +``` + +##### 2. Display picker at particular point on the screen and print current page to the selected printer +```javascript +cordova.plugins.printer.printerPicker(function (printerId) { + if(printerId) + { + // URI for the index.html + var page = location.href; + cordova.plugins.printer.print(page, {name: 'Document.html', printerId: printerId }, function () { + alert('Printing finished?') + }); + } + else + { + alert('Printer not selected'); + } +}, { bounds: { left:100, top:300, width:0, height: 0 } } ); +``` ## Quirks diff --git a/src/ios/APPPrinter.h b/src/ios/APPPrinter.h index 16634c4..7c80fc5 100755 --- a/src/ios/APPPrinter.h +++ b/src/ios/APPPrinter.h @@ -29,5 +29,7 @@ - (void) print:(CDVInvokedUrlCommand*)command; // Find out whether printing is supported on this platform - (void) isAvailable:(CDVInvokedUrlCommand*)command; +// Displays system interface for selecting a printer +- (void) printerPicker:(CDVInvokedUrlCommand*)command; @end diff --git a/src/ios/APPPrinter.m b/src/ios/APPPrinter.m index 601be16..8bc0f13 100755 --- a/src/ios/APPPrinter.m +++ b/src/ios/APPPrinter.m @@ -44,7 +44,7 @@ CDVPluginResult* pluginResult; BOOL isAvailable = [self isPrintingAvailable]; NSArray *multipart = @[[NSNumber numberWithBool:isAvailable], @[]]; - + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsMultipart:multipart]; @@ -93,6 +93,27 @@ } } +/** + * Displays system interface for selecting a printer + * + * @param command + * Contains the callback function and picker options if applicable + */ +- (void) printerPicker:(CDVInvokedUrlCommand*)command +{ + if (!self.isPrintingAvailable) { + return; + } + _callbackId = command.callbackId; + + NSArray* arguments = [command arguments]; + NSMutableDictionary* settings = [arguments objectAtIndex:0]; + + CGRect rect = [self convertIntoRect:[settings objectForKey:@"bounds"]]; + + [self presentPrinterPicker:rect]; +} + /** * Retrieves an instance of shared print controller. * @@ -297,4 +318,60 @@ isPrintingAvailable]; } +/** + * Displays system interface for selecting a printer + * + * @param rect + * Rect object of where to display the interface + */ +- (void) presentPrinterPicker:(CGRect)rect +{ + UIPrinterPickerController* controller= [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil]; + + if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) { + [controller presentFromRect:rect inView:self.webView animated:YES completionHandler: + ^(UIPrinterPickerController *ctrl, BOOL userDidSelect, NSError *e) { + [self returnPrinterPickerResult:ctrl withUserDidSelect:&userDidSelect]; + }]; + } + else { + [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 + * + * @param ctrl + * The UIPrinterPickerController used to display the printer selector interface + * @param userDidSelect + * True if the user selected a printer + */ +- (void) returnPrinterPickerResult:(UIPrinterPickerController*)ctrl + withUserDidSelect:(BOOL*)userDidSelect +{ + CDVPluginResult *pluginResult; + + if (userDidSelect) + { + UIPrinter* printer = ctrl.selectedPrinter; + [UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:printer]; + + pluginResult = [CDVPluginResult + resultWithStatus:CDVCommandStatus_OK + messageAsString:printer.URL.absoluteString]; + } + else + { + pluginResult = [CDVPluginResult + resultWithStatus:CDVCommandStatus_NO_RESULT]; + } + + [self.commandDelegate sendPluginResult:pluginResult + callbackId:_callbackId]; +} + @end diff --git a/www/printer.js b/www/printer.js index dcd8ca6..ef4ce44 100755 --- a/www/printer.js +++ b/www/printer.js @@ -59,6 +59,21 @@ exports.isAvailable = function (callback, scope) { exec(fn, null, 'Printer', 'isAvailable', []); }; +/** + * Displays system interface for selecting a printer (iOS only) + * + * @param {Function} callback + * A callback function + * @param {Object} options + * Options for the printer picker + */ +exports.printerPicker = function (callback, options) { + var fn = this._createCallbackFn(callback); + var params = options || {}; + params = this.mergeWithDefaults(params); + exec(fn, null, 'Printer', 'printerPicker', [params]); +}; + /** * Sends the content to the Google Cloud Print service. *