print2

Simple iOS Printing from AS3

Printing can be a complex process. However, a simple print function exists as a part of the iOS 6.0+ Social framework. It allows you to print images and strings with just one API call. To do this from Adobe AIR in ActionScript, we will need theĀ Social ANE from vitapoly. Setup Download the Social Air Native [...]

Printing can be a complex process. However, a simple print function exists as a part of the iOS 6.0+ Social framework. It allows you to print images and strings with just one API call. To do this from Adobe AIR in ActionScript, we will need theĀ Social ANE from vitapoly.

Setup

  1. Download the Social Air Native Extension
  2. Add the ANE to the project, it’s in “extensions/ane/Social.ane”
  3. Add in the sample code below
  4. Build and Run!

How it Works

AirPrint Compatible Printers

You will need an AirPrint compatible printer. There is a bunch of them, more details can be found on Apple’s AirPrint Basics guide.

Use the social.share API

social.share([
	"You can find the best Adobe Air Native Extensions at airextensions.net!",
	bitmapData]
);

Well, that’s it.

The share method accepts an array of parameters; it can only be strings or bitmapData. After that API call is invoked, an activity view will be created for the user. It supports a host of sharing options, one of them is print.

print1print2

The Full Source

You can copy and paste this into a new project. Be sure to add the Social ANE to the project!

package
{
	import com.vitapoly.nativeextensions.social.Social;
	import com.vitapoly.nativeextensions.social.events.ShareCompletedEvent;
 
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
 
	public class SocialTest extends Sprite
	{
		private var social:Social;
 
		private var debugTxt:TextField;
		private var defaultTextFormat:TextFormat = null;
 
		[Embed(source="airextensions_net.jpg")]
		private var Picture:Class;
		private var bitmapData:BitmapData = (new Picture()).bitmapData;
 
		public function SocialTest()
		{
			super();
 
			// support autoOrients
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
 
			// check if current platform is supported
			// this ANE works on iOS devices and iOS Simulator, but not the AIR Simulator
			if (Social.isSupported) {
 
				social = new Social();
 
				setupActivitySheet();
 
			}
		}
 
		// iOS 6.0 activity sheet
		private function setupActivitySheet():void {
			// listen to completed event
			social.addEventListener(Social.SHARE_COMPLETED_EVENT, function(e:ShareCompletedEvent):void {
				trace("share to " + e.activityType + " completed: " + e.completed);
			});
 
			addBtn("Activity Sheet", 0, 0, function(e:Event):void {
				// pass in a array of strings, urls, and/or BitmapData's
				social.share([
					"You can find the best Adobe Air Native Extensions at airextensions.net!",
					bitmapData]
				);
			});			
		}
 
		private function addBtn(text:String, x:Number, y:Number, onClick:Function):void {
			var btn:TextField = new TextField();
			btn.wordWrap = true;
			btn.text = "\n" + text;
			btn.x = x;
			btn.y = y;
			btn.width = 90;
			btn.height = 90;
			btn.border = true;
			btn.addEventListener(MouseEvent.CLICK, onClick);
			addChild(btn);
		}
	}
}

But I Want To Print More Complex Stuff!

There is actually a more complex printing framework that is part of the UIKit. You can print PDFs and even use your own custom print renderer! If you are interested in this framework, let us know in the comments. :-)