2013-08-10 15:50:42 +08:00
|
|
|
/**
|
|
|
|
* Printer.m
|
|
|
|
* 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
|
|
|
|
|
|
|
#import "CDVPrinter.h"
|
|
|
|
|
|
|
|
|
|
|
|
@implementation CDVPrinter
|
|
|
|
|
|
|
|
/*
|
2013-08-10 15:50:42 +08:00
|
|
|
* Is printing available.
|
|
|
|
* Callback returns true/false if printing is available/unavailable.
|
2013-08-10 00:58:47 +08:00
|
|
|
*/
|
2013-08-10 15:50:42 +08:00
|
|
|
- (void) isServiceAvailable:(CDVInvokedUrlCommand*)command {
|
|
|
|
CDVPluginResult *pluginResult = nil;
|
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.
|
|
|
|
*/
|
|
|
|
- (void) print:(CDVInvokedUrlCommand*)command {
|
|
|
|
NSArray *arguments = [command arguments];
|
|
|
|
CDVPluginResult *pluginResult = nil;
|
|
|
|
|
|
|
|
if (![self isPrintServiceAvailable]) {
|
|
|
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
|
|
|
messageAsString:@"{success: false, available: false}"];
|
|
|
|
|
|
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
|
|
|
2013-08-10 00:58:47 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
|
|
|
if ([arguments count] == 0) {
|
|
|
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
|
|
|
messageAsString:@"{success: false, available: true}"];
|
|
|
|
|
|
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
|
|
|
2013-08-10 00:58:47 +08:00
|
|
|
return;
|
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
|
|
|
NSString *content = [arguments objectAtIndex:0];
|
|
|
|
UIPrintInteractionController *controller = [self prepareController:content];
|
|
|
|
|
|
|
|
void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) =
|
|
|
|
^(UIPrintInteractionController *printController, BOOL completed, NSError *error) {
|
|
|
|
CDVPluginResult *pluginResult = nil;
|
|
|
|
|
|
|
|
if (!completed || error) {
|
|
|
|
NSString *result = [NSString stringWithFormat:@"{success: false, available: true, error: \"%@\"}", error.localizedDescription];
|
|
|
|
|
|
|
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR
|
|
|
|
messageAsString:result];
|
|
|
|
|
2013-08-10 00:58:47 +08:00
|
|
|
} else {
|
2013-08-10 15:50:42 +08:00
|
|
|
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
|
|
|
|
messageAsString:@"{success: true, available: true}"];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
2013-08-10 15:50:42 +08:00
|
|
|
|
|
|
|
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
|
|
|
|
};
|
|
|
|
|
|
|
|
[controller presentAnimated:YES completionHandler:completionHandler];
|
2013-08-10 00:58:47 +08:00
|
|
|
}
|
|
|
|
|
2013-08-10 15:50:42 +08:00
|
|
|
/**
|
|
|
|
* Bereitet den Drucker-Kontroller vor.
|
|
|
|
*
|
|
|
|
* @param {NSString} content Der zu druckende Inhalt
|
|
|
|
*/
|
|
|
|
- (UIPrintInteractionController *) prepareController:(NSString *)content {
|
|
|
|
UIPrintInteractionController *controller = [UIPrintInteractionController sharedPrintController];
|
|
|
|
|
|
|
|
//Set the priner settings
|
|
|
|
UIPrintInfo *printInfo = [UIPrintInfo printInfo];
|
|
|
|
printInfo.outputType = UIPrintInfoOutputGeneral;
|
|
|
|
controller.printInfo = printInfo;
|
|
|
|
controller.showsPageRange = YES;
|
|
|
|
|
|
|
|
//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 *webViewPrint = [[UIWebView alloc] init];
|
|
|
|
[webViewPrint loadHTMLString:content baseURL:baseURL];
|
|
|
|
|
|
|
|
//Get formatter for web (note: margin not required - done in web page)
|
|
|
|
UIViewPrintFormatter *viewFormatter = [webViewPrint viewPrintFormatter];
|
|
|
|
controller.printFormatter = viewFormatter;
|
|
|
|
controller.showsPageRange = YES;
|
|
|
|
|
|
|
|
return controller;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Überprüft, ob der Drucker-Dienst verfügbar ist.
|
|
|
|
*/
|
|
|
|
- (BOOL) isPrintServiceAvailable {
|
|
|
|
Class printController = NSClassFromString(@"UIPrintInteractionController");
|
|
|
|
|
|
|
|
if (printController) {
|
2013-08-10 00:58:47 +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
|