2013-08-10 15:50:42 +08:00
|
|
|
/**
|
2013-08-10 22:13:04 +08:00
|
|
|
* APPPrinter.m
|
2013-08-10 15:50:42 +08:00
|
|
|
* Cordova Printer Plugin
|
|
|
|
*
|
|
|
|
* Created by Sebastian Katzer (github.com/katzer) on 10/08/2013.
|
|
|
|
* Copyright 2013 Sebastian Katzer. All rights reserved.
|
|
|
|
* GPL v2 licensed
|
|
|
|
*/
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2013-08-10 22:13:04 +08:00
|
|
|
#import "APPPrinter.h"
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2013-08-25 02:59:49 +08:00
|
|
|
#define APP_PRINT_CANCELLED 0 // Printing cancelled (cancel button pressed)
|
|
|
|
#define APP_PRINT_SENT 2 // Page printed
|
|
|
|
#define APP_PRINT_FAILED 3 // Printing failed
|
|
|
|
#define APP_PRINT_NOTSENT 4 // Page not printed (something wrong happened)
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2013-08-11 17:37:59 +08:00
|
|
|
@interface APPPrinter (Private)
|
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
// Erstellt den PrintController
|
|
|
|
- (UIPrintInteractionController *) getPrintController;
|
|
|
|
// Stellt die Eigenschaften des Druckers ein.
|
|
|
|
- (UIPrintInteractionController *) adjustSettingsForPrintController:(UIPrintInteractionController *)controller;
|
|
|
|
// Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll.
|
2013-08-23 04:42:28 +08:00
|
|
|
- (void) loadContent:(NSString *)content intoPrintController:(UIPrintInteractionController *)controller;
|
2013-08-13 21:39:22 +08:00
|
|
|
// Ruft den Callback auf und informiert diesen über den das Ergebnis des Druckvorgangs.
|
2013-08-25 02:59:49 +08:00
|
|
|
- (void) informAboutResult:(int)code callbackId:(NSString *)callbackId;
|
2013-08-11 17:37:59 +08:00
|
|
|
// Überprüft, ob der Drucker-Dienst verfügbar ist
|
|
|
|
- (BOOL) isPrintServiceAvailable;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2013-08-10 22:13:04 +08:00
|
|
|
@implementation APPPrinter
|
2013-08-10 00:58:47 +08:00
|
|
|
|
|
|
|
/*
|
2013-08-10 15:50:42 +08:00
|
|
|
* Is printing available.
|
2013-08-10 00:58:47 +08:00
|
|
|
*/
|
2013-08-13 17:01:15 +08:00
|
|
|
- (void) isServiceAvailable:(CDVInvokedUrlCommand *)command
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2013-08-13 21:39:22 +08:00
|
|
|
CDVPluginResult* pluginResult;
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
|
messageAsBool:[self isPrintServiceAvailable]];
|
2013-08-10 00:58:47 +08:00
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
/**
|
|
|
|
* Öffnet den Drucker-Kontroller zur Auswahl des Druckers.
|
|
|
|
* Callback gibt Meta-Informationen an.
|
|
|
|
*/
|
2013-08-13 17:01:15 +08:00
|
|
|
- (void) print:(CDVInvokedUrlCommand *)command
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2013-08-11 17:43:58 +08:00
|
|
|
if (![self isPrintServiceAvailable])
|
|
|
|
{
|
2013-08-25 02:59:49 +08:00
|
|
|
[self informAboutResult:APP_PRINT_FAILED callbackId:command.callbackId];
|
2013-08-10 00:58:47 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
NSArray* arguments = [command arguments];
|
|
|
|
NSString* content = [arguments objectAtIndex:0];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
UIPrintInteractionController* controller = [self getPrintController];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
[self adjustSettingsForPrintController:controller];
|
2013-08-23 04:42:28 +08:00
|
|
|
[self loadContent:content intoPrintController:controller];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
[controller presentAnimated:YES completionHandler:^(UIPrintInteractionController* printController, BOOL completed, NSError* error) {
|
2013-08-25 02:59:49 +08:00
|
|
|
if (completed) {
|
|
|
|
[self informAboutResult:APP_PRINT_SENT callbackId:command.callbackId];
|
|
|
|
} else if (error) {
|
|
|
|
[self informAboutResult:APP_PRINT_FAILED callbackId:command.callbackId];
|
|
|
|
} else {
|
|
|
|
[self informAboutResult:APP_PRINT_CANCELLED callbackId:command.callbackId];
|
|
|
|
}
|
2013-08-13 21:39:22 +08:00
|
|
|
}];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
/**
|
2013-08-13 21:39:22 +08:00
|
|
|
* Erstellt den PrintController.
|
2013-08-10 15:50:42 +08:00
|
|
|
*/
|
2013-08-13 21:39:22 +08:00
|
|
|
- (UIPrintInteractionController *) getPrintController
|
2013-08-11 17:35:40 +08:00
|
|
|
{
|
2013-08-13 21:39:22 +08:00
|
|
|
return [UIPrintInteractionController sharedPrintController];
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
/**
|
|
|
|
* Stellt die Eigenschaften des Druckers ein.
|
|
|
|
*/
|
|
|
|
- (UIPrintInteractionController *) adjustSettingsForPrintController:(UIPrintInteractionController *)controller
|
|
|
|
{
|
|
|
|
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
|
2013-08-10 15:50:42 +08:00
|
|
|
printInfo.outputType = UIPrintInfoOutputGeneral;
|
|
|
|
controller.printInfo = printInfo;
|
|
|
|
controller.showsPageRange = YES;
|
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
return controller;
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
/**
|
|
|
|
* Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll.
|
|
|
|
*/
|
2013-08-23 04:42:28 +08:00
|
|
|
- (void) loadContent:(NSString *)content intoPrintController:(UIPrintInteractionController *)controller
|
2013-08-13 21:39:22 +08:00
|
|
|
{
|
|
|
|
// Set the base URL to be the www directory.
|
|
|
|
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www" ofType:nil];
|
|
|
|
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
|
|
|
|
// Load page into a webview and use its formatter to print the page
|
|
|
|
UIWebView* webPage = [[UIWebView alloc] init];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
[webPage loadHTMLString:content baseURL:baseURL];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
// Get formatter for web (note: margin not required - done in web page)
|
|
|
|
UIViewPrintFormatter* formatter = [webPage viewPrintFormatter];
|
|
|
|
|
|
|
|
controller.printFormatter = formatter;
|
|
|
|
controller.showsPageRange = YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ruft den Callback auf und informiert diesen über den das Ergebnis des Druckvorgangs.
|
|
|
|
*/
|
2013-08-25 02:59:49 +08:00
|
|
|
- (void) informAboutResult:(int)code callbackId:(NSString *)callbackId
|
2013-08-13 21:39:22 +08:00
|
|
|
{
|
|
|
|
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
2013-08-25 02:59:49 +08:00
|
|
|
messageAsInt:code];
|
2013-08-13 21:39:22 +08:00
|
|
|
|
|
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
|
2013-08-10 15:50:42 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Überprüft, ob der Drucker-Dienst verfügbar ist.
|
|
|
|
*/
|
2013-08-11 17:35:40 +08:00
|
|
|
- (BOOL) isPrintServiceAvailable
|
|
|
|
{
|
2013-08-10 15:50:42 +08:00
|
|
|
Class printController = NSClassFromString(@"UIPrintInteractionController");
|
|
|
|
|
2013-08-11 17:43:58 +08:00
|
|
|
if (printController)
|
|
|
|
{
|
2013-08-13 17:01:15 +08:00
|
|
|
UIPrintInteractionController* controller = [UIPrintInteractionController sharedPrintController];
|
2013-08-10 15:50:42 +08:00
|
|
|
|
2013-08-10 00:58:47 +08:00
|
|
|
return (controller != nil) && [UIPrintInteractionController isPrintingAvailable];
|
|
|
|
}
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
return NO;
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|