cordova-plugin-printer/src/ios/APPPrinter.m

155 lines
5.1 KiB
Mathematica
Raw Normal View History

/**
2013-08-10 22:13:04 +08:00
* APPPrinter.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 22:13:04 +08:00
#import "APPPrinter.h"
@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.
- (void) informAboutResult:(BOOL)success serviceAvailable:(BOOL)available error:(NSString *)error callbackId:(NSString *)callbackId;
// Überprüft, ob der Drucker-Dienst verfügbar ist
- (BOOL) isPrintServiceAvailable;
@end
2013-08-10 22:13:04 +08:00
@implementation APPPrinter
/*
* Is printing available.
*/
- (void) isServiceAvailable:(CDVInvokedUrlCommand *)command
2013-08-11 17:35:40 +08:00
{
2013-08-13 21:39:22 +08:00
CDVPluginResult* pluginResult;
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:[self isPrintServiceAvailable]];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
}
/**
* Öffnet den Drucker-Kontroller zur Auswahl des Druckers.
* Callback gibt Meta-Informationen an.
*/
- (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-13 21:39:22 +08:00
[self informAboutResult:FALSE serviceAvailable:FALSE error:NULL callbackId:command.callbackId];
return;
}
2013-08-13 21:39:22 +08:00
NSArray* arguments = [command arguments];
NSString* content = [arguments objectAtIndex:0];
2013-08-13 21:39:22 +08:00
UIPrintInteractionController* controller = [self getPrintController];
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-13 21:39:22 +08:00
[controller presentAnimated:YES completionHandler:^(UIPrintInteractionController* printController, BOOL completed, NSError* error) {
[self informAboutResult:completed serviceAvailable:TRUE error:(error ? error.localizedDescription : @"null") callbackId:command.callbackId];
}];
}
/**
2013-08-13 21:39:22 +08:00
* Erstellt den PrintController.
*
2013-08-13 21:39:22 +08:00
* @return {UIPrintInteractionController *}
*/
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-13 21:39:22 +08:00
/**
* Stellt die Eigenschaften des Druckers ein.
*
* @param {UIPrintInteractionController *} controller
*/
- (UIPrintInteractionController *) adjustSettingsForPrintController:(UIPrintInteractionController *)controller
{
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
printInfo.outputType = UIPrintInfoOutputGeneral;
controller.printInfo = printInfo;
controller.showsPageRange = YES;
2013-08-13 21:39:22 +08:00
return controller;
}
2013-08-13 21:39:22 +08:00
/**
* Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll.
*
* @param {NSString} content
* @param {UIPrintInteractionController *} controller
*/
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-13 21:39:22 +08:00
[webPage loadHTMLString:content baseURL:baseURL];
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.
*
* @param {NSString *} callbackId
* @param {Boolean} success
* @param {Boolean} available
* @param {NSString *} error
*/
- (void) informAboutResult:(BOOL)success serviceAvailable:(BOOL)available error:(NSString *)error callbackId:(NSString *)callbackId
{
NSString* wasSuccess = success ? @"true" : @"false";
NSString* wasAvailable = available ? @"true" : @"false";
NSString* result = [NSString stringWithFormat:@"%@, %@, %@", wasSuccess, wasAvailable, error];
CDVPluginResult* pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsString:result];
[self.commandDelegate sendPluginResult:pluginResult callbackId:callbackId];
}
/**
* Überprüft, ob der Drucker-Dienst verfügbar ist.
*/
2013-08-11 17:35:40 +08:00
- (BOOL) isPrintServiceAvailable
{
Class printController = NSClassFromString(@"UIPrintInteractionController");
2013-08-11 17:43:58 +08:00
if (printController)
{
UIPrintInteractionController* controller = [UIPrintInteractionController sharedPrintController];
return (controller != nil) && [UIPrintInteractionController isPrintingAvailable];
}
return NO;
}
@end