Camera Tutorial, Part 2: Set up the Xcode project

Opt In Image
Early bird offer on the ANE eBooks

 

Buy any Easy Native Extensions 2nd Edition package and get our $99 iOS + Android ANE Template completely free before the end of June 2015.

 

 

  • step-by-step guide to making your iOS extension in under an hour
  • library for data conversion between ActionScript and native code
  • tutorials
  • infographics
  • code included

At the end of this part you will have:

An Xcode project with AIR interface and function placeholders for driving the camera which you’ll fill in the next few parts of the tutorial.

Time

8-10 miutes

Wait, have you done this first?

  • Part 1: Create a test app – 15-20 minutes
  • You don’t necessarily need a test app to complete this part of the tutorial, but it gives you an idea of what you’ll need from the ANE and a way of testing your code as you write it. Go on, set up your app, I’ll wait for you here.

Step 1: Create an iOS library in Xcode

In Xcode’s menu select File > New >Project, then under iOS select Framework & Library > Cocoa Touch Static Library.

01 xcode project 01

 Name your project and save it. I have named my project CameraLibiOS and this is what Xcode creates for me in Project Navigator:

Create an Cocoa Touch library in Xcode

For the iOS library we won’t need the header file CameraLibiOS.h, so feel free to delete it (Right-click > Delete). The source file CameraLibiOS.m will be the focus of our work today – read on to see why.

Step 2: Add the AIR SDK

On iOS all you need is to add the AIR SDK header, FlashRuntimeExtensions.h, to your project. I like to put the AIR SDK header under Supporting Files in Project Navigator – it doesn’t really matter where it sits in the project structure, though.

Right-click Supporting files and select Add files to “CameraLibiOS…”, then navigate to where the AIR SDK is installed on your machine, very likely here:

If you are unsure whether to check Copy items into destination group’s folder (if needed), have a look at this article: Do you need a copy of FlashRuntimeExtensions.h in your project?

Step 3: Add the Native Extension initializer and finalizer

Open CameraLibiOS.m. In this file we’ll put the code that ActionScript can call – this will be the interface of your native library.

Xcode has helpfully put a class implementation there for you:

Very nice of you, Xcode, but we won’t be using that – you don’t really need a class for our purposes here. Exterminate. Delete. Delete. Sorry. Couldn’t resist.

Starting with a clean slate, import the AIR SDK header first:

Next, let’s add the extension initializer:

This is the function that AIR will call first when your ANE is loaded in memory. Have a look at this article for details on what each line of code means: Extension Initializer and Finalizer in C.

If you copy and paste that function in Xcode you’ll notice that it underlines the last two lines as errors:

Semantic issue: Use of undeclared identifier ‘CameraLibContextInitializer’; did you mean ‘CameraLibInitializer’?

Semantic issue: Use of undeclared identifier ‘CameraLibContextFinalizer’; did you mean ‘CameraLibInitializer’?

Let Xcode complain and don’t allow it to think you meant ‘CameraLibInitializer’ in either case. CameraLibContextInitializer and CameraLibContextFinalizer are the names of your context initializer and finalizer functions, which you’ll add in a minute.

Before that, add your extension finalizer:

This is the function that AIR calls when your ANE is unloaded from memory. See how that works in this article: Unloading your ANE.

Step 4: Add the Context initializer and finalizer

Here you will implement CameraLibContextInitializer and CameraLibContextFinalizer that we promised Xcode a few moments ago:

The context initializer and finalizer are called when you instantiate an instance of flash.external.ExtensionContext in your ActionScript code and when you dispose of it. Here is a reminder of what the Extension Context and its initializer and finalizer are, if you need it.

The next thing that happens after you add the code above is that Xcode complains about not knowing what g_ctx, ASStartCameraPreview and ASStopCameraPreview are. Let’s fix that. First, define g_ctx at the top of your file, right after where you import FlashRunTimeExtensions.h:

We’ll add the other missing declarations in Step 5.

Step 5: Add the AIR interface

Although you don’t know yet exactly how we’ll obtain a camera preview and pass it to ActionScript, you have two pieces of functionality you can add at this point: you’ll want native code to start and stop the camera:

These two functions are placeholders. We’ll be returning to fill them in with functionality in Part 5: Start the camera from ActionScript and  Part 8: Stop the camera.

Step 6: Add a way of sending events to AIR

You will need a way of telling ActionScript if something happens outside one of the functions you have just declared. Add a function that will do just that:

There is a specific reason why we declare sendMessage() like this. If you are curious, see Keeping your C native code reusable and independent of AIR. And if you are new to how AIR and native code talk to one another, have a look at Communication between ActionScript and native code in an ANE.

Step 7: Make sure your library builds

You won’t be able to run any of this code just yet, but let’s make sure it builds OK. In Xcode select Product > Build (or press Command + B). This should produce libCameraLibiOS.a under Products:

Build the Xcode libraryIf you get any build errors, work through them one by one. Xcode will tell you what its problem is. Alternatively, shout in the comments below and we’ll do our best to help.

What’s next?

Helpful reading

If this is the first time you are writing an Xcode library to be used by AIR, here is a list of articles that will help you make sense of what we have just done:

Wait, want more features and Android support?

Check out the DiaDraw Camera Driver ANE.


Warning: count(): Parameter must be an array or an object that implements Countable in /home/easyna6/public_html/easynativeextensions_wp/wp-includes/class-wp-comment-query.php on line 399

Comments

  1. Radoslava

    Hi Pornphop,

    In doubleroboticExtension.m the context initializer and finalizer need to be defined before the extension initializer and finalizer: i.e. DRDoubleLibContextInitializer and DRDoubleLibContextFinalizer need to be above DRDoubleLibInitializer, so it can see them.

    Radoslava

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">