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

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