2013-12-11 20:00:16 +08:00
|
|
|
/*
|
2013-12-11 20:21:23 +08:00
|
|
|
Copyright 2013 appPlant UG
|
|
|
|
|
|
|
|
Licensed to the Apache Software Foundation (ASF) under one
|
|
|
|
or more contributor license agreements. See the NOTICE file
|
|
|
|
distributed with this work for additional information
|
|
|
|
regarding copyright ownership. The ASF licenses this file
|
|
|
|
to you under the Apache License, Version 2.0 (the
|
|
|
|
"License"); you may not use this file except in compliance
|
|
|
|
with the License. You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing,
|
|
|
|
software distributed under the License is distributed on an
|
|
|
|
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
|
|
KIND, either express or implied. See the License for the
|
|
|
|
specific language governing permissions and limitations
|
|
|
|
under the License.
|
|
|
|
*/
|
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-11 17:37:59 +08:00
|
|
|
@interface APPPrinter (Private)
|
|
|
|
|
2013-08-13 21:39:22 +08:00
|
|
|
// Erstellt den PrintController
|
2013-12-11 20:21:23 +08:00
|
|
|
- (UIPrintInteractionController*) getPrintController;
|
2013-08-13 21:39:22 +08:00
|
|
|
// Stellt die Eigenschaften des Druckers ein.
|
2013-12-11 20:21:23 +08:00
|
|
|
- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller;
|
2013-08-13 21:39:22 +08:00
|
|
|
// Lädt den zu druckenden Content in ein WebView, welcher vom Drucker ausgedruckt werden soll.
|
2013-12-11 20:21:23 +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-12-11 20:21:23 +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-12-11 20:21:23 +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
|
2013-12-11 20:38:01 +08:00
|
|
|
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-12-11 20:21:23 +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-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-12-11 20:38:01 +08:00
|
|
|
[self openPrintController:controller];
|
2013-12-11 20:21:23 +08:00
|
|
|
|
|
|
|
[self commandDelegate];
|
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-12-11 20:21:23 +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.
|
|
|
|
*/
|
2013-12-11 20:21:23 +08:00
|
|
|
- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller
|
2013-08-13 21:39:22 +08:00
|
|
|
{
|
|
|
|
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-12-11 20:21:23 +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;
|
|
|
|
}
|
|
|
|
|
2013-12-11 20:38:01 +08:00
|
|
|
/**
|
|
|
|
* Zeigt den PrintController an.
|
|
|
|
*/
|
|
|
|
- (void) openPrintController:(UIPrintInteractionController*)controller
|
|
|
|
{
|
|
|
|
//[self.commandDelegate runInBackground:^{
|
|
|
|
[controller presentAnimated:YES completionHandler:NULL];
|
|
|
|
//}];
|
|
|
|
}
|
|
|
|
|
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
|