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

398 lines
12 KiB
Mathematica
Raw Normal View History

2013-12-11 20:00:16 +08:00
/*
2016-07-21 03:00:33 +08:00
Copyright 2013-2016 appPlant GmbH
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"
2014-10-08 03:20:09 +08:00
#import <Cordova/CDVAvailability.h>
2014-09-08 04:52:36 +08:00
@interface APPPrinter ()
2014-09-08 04:52:36 +08:00
@property (retain) NSString* callbackId;
@property (retain) NSMutableDictionary* settings;
@end
2013-08-10 22:13:04 +08:00
@implementation APPPrinter
2016-07-25 21:04:20 +08:00
#pragma mark -
#pragma mark Interface
/*
2014-09-08 04:52:36 +08:00
* Checks if the printing service is available.
*
* @param {Function} callback
* A callback function to be called with the result
*/
2014-09-08 04:52:36 +08:00
- (void) isAvailable:(CDVInvokedUrlCommand*)command
2013-08-11 17:35:40 +08:00
{
[self.commandDelegate runInBackground:^{
CDVPluginResult* pluginResult;
BOOL isAvailable = [self isPrintingAvailable];
NSArray *multipart = @[[NSNumber numberWithBool:isAvailable], @[]];
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsMultipart:multipart];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}];
}
/**
2014-09-08 04:52:36 +08:00
* Sends the printing content to the printer controller and opens them.
*
* @param {NSString} content
* The (HTML encoded) content
*/
- (void) print:(CDVInvokedUrlCommand*)command
2013-08-11 17:35:40 +08:00
{
2014-09-08 04:52:36 +08:00
if (!self.isPrintingAvailable) {
return;
}
2014-09-08 04:52:36 +08:00
_callbackId = command.callbackId;
2016-07-25 21:04:20 +08:00
NSArray* arguments = [command arguments];
2014-09-08 04:52:36 +08:00
NSString* content = [arguments objectAtIndex:0];
2016-07-25 21:04:20 +08:00
self.settings = [arguments objectAtIndex:1];
2014-09-08 04:52:36 +08:00
UIPrintInteractionController* controller = [self printController];
[self adjustPrintController:controller withSettings:self.settings];
2014-09-08 04:52:36 +08:00
[self loadContent:content intoPrintController:controller];
}
2016-07-25 21:04:20 +08:00
/**
* Displays system interface for selecting a printer
*
* @param command
* Contains the callback function and picker options if applicable
*/
- (void) pick:(CDVInvokedUrlCommand*)command
{
if (!self.isPrintingAvailable) {
return;
}
_callbackId = command.callbackId;
NSArray* arguments = [command arguments];
NSMutableDictionary* settings = [arguments objectAtIndex:0];
NSArray* bounds = [settings objectForKey:@"bounds"];
CGRect rect = [self convertIntoRect:bounds];
[self presentPrinterPicker:rect];
}
#pragma mark -
#pragma mark UIWebViewDelegate
/**
* Sent after a web view finishes loading a frame.
*
* @param webView
* The web view has finished loading.
*/
- (void) webViewDidFinishLoad:(UIWebView *)webView
{
UIPrintInteractionController* controller = [self printController];
NSString* printerId = [self.settings objectForKey:@"printerId"];
2016-07-21 03:00:33 +08:00
2016-07-25 21:04:20 +08:00
if (( ![printerId isEqual:[NSNull null]] ) && ( [printerId length] > 0 )) {
[self sendToPrinter:controller printer:printerId];
2016-07-25 21:04:20 +08:00
return;
}
2016-07-21 03:00:33 +08:00
2016-07-25 21:04:20 +08:00
NSArray* bounds = [self.settings objectForKey:@"bounds"];
CGRect rect = [self convertIntoRect:bounds];
[self presentPrintController:controller fromRect:rect];
}
#pragma mark -
#pragma mark Core
/**
* Checks either the printing service is avaible or not.
*
* @return {BOOL}
*/
- (BOOL) isPrintingAvailable
{
Class controllerCls = NSClassFromString(@"UIPrintInteractionController");
if (!controllerCls) {
return NO;
}
2016-07-25 21:04:20 +08:00
return [self printController] && [UIPrintInteractionController
isPrintingAvailable];
}
/**
2016-07-25 21:04:20 +08:00
* Opens the print controller so that the user can choose between
* available iPrinters.
*
2016-07-25 21:04:20 +08:00
* @param {UIPrintInteractionController} controller
* The prepared print controller with a content
*/
2016-07-25 21:04:20 +08:00
- (void) presentPrintController:(UIPrintInteractionController*)controller
fromRect:(CGRect)rect
{
2016-07-25 21:04:20 +08:00
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromRect:rect inView:self.webView animated:YES completionHandler:
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
CDVPluginResult* pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:ok];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}];
}
2016-07-25 21:04:20 +08:00
else {
[controller presentAnimated:YES completionHandler:
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
CDVPluginResult* pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:ok];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}];
}
}
2016-07-25 21:04:20 +08:00
/**
* Sends the content directly to the specified printer.
*
* @param controller
* The prepared print controller with the content
* @param printer
* The printer specified by its URL
*/
- (void) sendToPrinter:(UIPrintInteractionController*)controller
printer:(NSString*)printerId
{
NSURL* url = [NSURL URLWithString:printerId];
UIPrinter* printer = [UIPrinter printerWithURL:url];
[controller printToPrinter:printer completionHandler:
^(UIPrintInteractionController *ctrl, BOOL ok, NSError *e) {
CDVPluginResult* pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_OK
messageAsBool:ok];
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}];
}
2016-07-25 21:04:20 +08:00
/**
* Displays system interface for selecting a printer
*
* @param rect
* Rect object of where to display the interface
*/
- (void) presentPrinterPicker:(CGRect)rect
{
UIPrinterPickerController* controller =
[UIPrinterPickerController printerPickerControllerWithInitiallySelectedPrinter:nil];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
[controller presentFromRect:rect inView:self.webView animated:YES completionHandler:
^(UIPrinterPickerController *ctrl, BOOL userDidSelect, NSError *e) {
[self returnPrinterPickerResult:ctrl
withUserDidSelect:&userDidSelect];
}];
}
else {
[controller presentAnimated:YES completionHandler:
^(UIPrinterPickerController *ctrl, BOOL userDidSelect, NSError *e) {
[self returnPrinterPickerResult:ctrl
withUserDidSelect:&userDidSelect];
}];
}
}
2016-07-25 21:04:20 +08:00
/**
* Calls the callback funtion with the result of the selected printer
*
* @param ctrl
* The UIPrinterPickerController used to display the printer selector interface
* @param userDidSelect
* True if the user selected a printer
*/
- (void) returnPrinterPickerResult:(UIPrinterPickerController*)ctrl
withUserDidSelect:(BOOL*)userDidSelect
{
CDVPluginResult* pluginResult =
[CDVPluginResult resultWithStatus:CDVCommandStatus_NO_RESULT];
if (userDidSelect) {
UIPrinter* printer = ctrl.selectedPrinter;
[UIPrinterPickerController
printerPickerControllerWithInitiallySelectedPrinter:printer];
pluginResult = [CDVPluginResult
resultWithStatus:CDVCommandStatus_OK
messageAsString:printer.URL.absoluteString];
}
[self.commandDelegate sendPluginResult:pluginResult
callbackId:_callbackId];
}
2016-07-25 21:04:20 +08:00
#pragma mark -
#pragma mark Helper
/**
2014-09-08 04:52:36 +08:00
* Retrieves an instance of shared print controller.
*
* @return {UIPrintInteractionController*}
*/
2014-09-08 04:52:36 +08:00
- (UIPrintInteractionController*) printController
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
/**
2014-09-08 04:52:36 +08:00
* Adjusts the settings for the print controller.
*
* @param {UIPrintInteractionController} controller
* The print controller instance
*
* @return {UIPrintInteractionController} controller
* The modified print controller instance
2013-08-13 21:39:22 +08:00
*/
2014-09-08 04:52:36 +08:00
- (UIPrintInteractionController*) adjustPrintController:(UIPrintInteractionController*)controller
withSettings:(NSMutableDictionary*)settings
2013-08-13 21:39:22 +08:00
{
2014-09-08 04:52:36 +08:00
UIPrintInfo* printInfo = [UIPrintInfo printInfo];
UIPrintInfoOrientation orientation = UIPrintInfoOrientationPortrait;
UIPrintInfoOutputType outputType = UIPrintInfoOutputGeneral;
UIPrintInfoDuplex duplexMode = UIPrintInfoDuplexNone;
2014-09-08 04:52:36 +08:00
if ([[settings objectForKey:@"landscape"] boolValue]) {
orientation = UIPrintInfoOrientationLandscape;
}
if ([[settings objectForKey:@"graystyle"] boolValue]) {
outputType = UIPrintInfoOutputGrayscale;
}
if ([[settings objectForKey:@"duplex"] isEqualToString:@"long"]) {
duplexMode = UIPrintInfoDuplexLongEdge;
} else
if ([[settings objectForKey:@"duplex"] isEqualToString:@"short"]) {
duplexMode = UIPrintInfoDuplexShortEdge;
}
2014-09-08 04:52:36 +08:00
printInfo.outputType = outputType;
printInfo.orientation = orientation;
printInfo.duplex = duplexMode;
2014-09-08 04:52:36 +08:00
printInfo.jobName = [settings objectForKey:@"name"];
controller.printInfo = printInfo;
controller.showsPageRange = ![[settings objectForKey:@"hidePageRange"] boolValue];
controller.showsNumberOfCopies = ![[settings objectForKey:@"hideNumberOfCopies"] boolValue];
controller.showsPaperSelectionForLoadedPapers = ![[settings objectForKey:@"hidePaperFormat"] boolValue];
2013-08-13 21:39:22 +08:00
return controller;
}
2013-08-13 21:39:22 +08:00
/**
2014-09-08 04:52:36 +08:00
* Adjusts the web view and page renderer.
*/
- (void) adjustWebView:(UIWebView*)page
andPrintPageRenderer:(UIPrintPageRenderer*)renderer
2014-09-08 04:52:36 +08:00
{
UIViewPrintFormatter* formatter = [page viewPrintFormatter];
// margin not required - done in web page
formatter.contentInsets = UIEdgeInsetsMake(0.0f, 0.0f, 0.0f, 0.0f);
renderer.headerHeight = -30.0f;
renderer.footerHeight = -30.0f;
[renderer addPrintFormatter:formatter startingAtPageAtIndex:0];
page.scalesPageToFit = YES;
page.dataDetectorTypes = UIDataDetectorTypeNone;
page.userInteractionEnabled = NO;
page.autoresizingMask = (UIViewAutoresizingFlexibleWidth |
UIViewAutoresizingFlexibleHeight);
}
/**
* Loads the content into the print controller.
*
* @param {NSString} content
* The (HTML encoded) content
* @param {UIPrintInteractionController} controller
* The print controller instance
2013-08-13 21:39:22 +08:00
*/
- (void) loadContent:(NSString*)content intoPrintController:(UIPrintInteractionController*)controller
2013-08-13 21:39:22 +08:00
{
2014-09-08 04:52:36 +08:00
UIWebView* page = [[UIWebView alloc] init];
page.delegate = self;
2014-09-08 04:52:36 +08:00
UIPrintPageRenderer* renderer = [[UIPrintPageRenderer alloc] init];
[self adjustWebView:page andPrintPageRenderer:renderer];
if ([NSURL URLWithString:content]) {
NSURL *url = [NSURL URLWithString:content];
[page loadRequest:[NSURLRequest requestWithURL:url]];
}
else {
// Set the base URL to be the www directory.
NSString* wwwFilePath = [[NSBundle mainBundle] pathForResource:@"www"
ofType:nil];
NSURL* baseURL = [NSURL fileURLWithPath:wwwFilePath];
[page loadHTMLString:content baseURL:baseURL];
}
2013-08-13 21:39:22 +08:00
2014-09-08 04:52:36 +08:00
controller.printPageRenderer = renderer;
2013-08-13 21:39:22 +08:00
}
2014-10-14 05:31:12 +08:00
/**
* Convert Array into Rect object.
*
* @param bounds
* The bounds
*
* @return
* A converted Rect object
*/
- (CGRect) convertIntoRect:(NSArray*)bounds
{
return CGRectMake([[bounds objectAtIndex:0] floatValue],
[[bounds objectAtIndex:1] floatValue],
[[bounds objectAtIndex:2] floatValue],
[[bounds objectAtIndex:3] floatValue]);
}
@end