Merge branch 'master' of https://github.com/nkjf5/cordova-plugin-printer into nkjf5-master

This commit is contained in:
Sebastián Katzer
2016-07-25 14:19:06 +02:00
4 changed files with 130 additions and 1 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

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