Added support for Apple's UIPrinterPickerController

This commit is contained in:
Justin Fisher 2015-06-23 12:58:41 -05:00
parent 517dc364e8
commit 6d864a32a7
4 changed files with 123 additions and 0 deletions

View File

@ -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

View File

@ -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

View File

@ -86,6 +86,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.
*
@ -275,4 +296,54 @@
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(CDV_IsIPad()) {
[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
{
if (userDidSelect)
{
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:ctrl.selectedPrinter];
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ctrl.selectedPrinter.URL.absoluteString];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}
else
{
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
[self.commandDelegate sendPluginResult:pluginResult callbackId:_callbackId];
}
}
@end

View File

@ -57,6 +57,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.
*