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

140 lines
4.4 KiB
Mathematica
Raw Normal View History

2013-12-11 20:00:16 +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 22:13:04 +08:00
#import "APPPrinter.h"
@interface APPPrinter (Private)
2013-08-13 21:39:22 +08:00
// Erstellt den PrintController
- (UIPrintInteractionController*) getPrintController;
2013-08-13 21:39:22 +08:00
// Stellt die Eigenschaften des Druckers ein.
- (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.
- (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:(int)code 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])
{
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) {
2013-08-13 21:39:22 +08:00
}];
[self commandDelegate];
}
/**
2013-08-13 21:39:22 +08:00
* Erstellt den PrintController.
*/
- (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.
*/
- (UIPrintInteractionController*) adjustSettingsForPrintController:(UIPrintInteractionController*)controller
2013-08-13 21:39:22 +08:00
{
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.
*/
- (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;
}
/**
* Ü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