Chapter 3

Understand Your Options: Using Third-Party Solutions and MonoTouch

In previous chapters, we've looked at the iOS operating system, the SDK and associated tools, and how you can use Xcode 4 with the iOS SDK to develop your first iPhone application. But as described in Chapter 1, options other than Apple's native tools are available for developing applications. This chapter elaborates on some of the third-party options introduced in Chapter 1 and describes how to create the Hello, World application using them.

We won't cover all of the third-party options available, not least because of space considerations, but also because it's a constantly changing environment, with new options being introduced all the time. In this chapter, we'll focus on some of the more common and longer-standing options:

  • Using the .NET Framework with MonoTouch
  • Using JavaScript with Appcelerator'sTitanium Studio and Mobile SDK
  • Using Xcode with the Marmalade SDK

After you've become comfortable with developing our Hello, World application using each of the options covered in this chapter, we can then look at different elements of the iOS and a more advanced application in more detail in the remainder of the book.

Understanding the Constraints

Before we get started with the third-party options, it's worth understanding some principle constraints:

  • Native apps require a Mac: To write and execute native iOS applications, you will need a Mac.
  • SDK completeness: Third-party options such as Mono provide a layer on top of the existing iOS SDK, but often have not gotten around to implementing all of its functionality. So if you want to have a complete implementation of the API, you need to either work around gaps in a third-party's implementation or attempt to write your own implementation.
  • Speed: If you want to fully exploit the power of your iOS device, there is no substitute for writing your application in Objective-C directly on top of the SDK. While other options work, they introduce layers of abstraction that will slow down your application.

Having painted a relatively bleak picture of the third-party options, it is important to point out that some are extremely viable. This is especially true of the Mono implementation, which offers the most complete implementation of the iOS SDK's API in a familiar language. Speed is typically not an issue.

Developing with Mono and MonoTouch

You should know by now that the focus of this book is to support you in developing applications for Apple's mobile devices using a variety of methods, but with an emphasis on reusing your existing .NET knowledge and skills.

The success of the .NET Framework on the Microsoft Windows operating system didn't go unnoticed within the open source community. There are many who wished that .NET was available for not only Windows, but also for other operating systems, such as Linux and Mac OS X. Thankfully, someone did something about this. Miguel de Icaza took it upon himself, and then with community support, to bring .NET to these operating systems, allowing you write .NET applications for a multitude of platforms. And it didn't stop there. Soon the SharpDevelop IDE was ported to Mono, and MonoDevelop was formed, creating a Visual Studio-type IDE that is able to target not only .NET applications, but also C, C++, Python, and Java.

So, you have the .NET Framework, support for .NET languages such as C#, and a powerful IDE, and it was only logical for the mobile revolution to be supported! In fact, they went further. Not only was MonoTouch introduced, providing a mechanism to create C#- and .NET-based applications for the iPhone, but they also introduced MonoMac. MonoMac is an interesting development for creating Cocoa applications on Mac OS X using Mono. (For more information, visit http://www.mono-project.com/MonoMac.) MonoDroid has also been released, based on the same principles as MonoTouch but to target the Android operating system.

Enough of MonoMac and MonoDroid; let's look at MonoTouch. As stated, it allows you to use Mono to develop applications for the iPhone, iPad, and iPod touch with the API bindings to the iOS APIs.

Given that it's based on the .NET Framework and supports the C# programming language, you .NET guys and girls should be right at home! Let's take a look at obtaining, installing, and using the necessary components to write our Hello, World iPhone app using the Mono tools.

Installing Mono, MonoDevelop, and MonoTouch

To get started, you'll need to download and install the following components. These components can be accessed from the Mono projects home page (http://www.mono-project.com/).

  • Mono: This is the actual Mono framework. It includes the open source implementation of the .NET Framework, language support, and supporting tools. You can download Mono from http://www.go-mono.com/mono-downloads/download.html.
  • MonoDevelop: This is the IDE primarily designed for .NET and its languages such as C#. We'll be using this in the same way as we used Xcode in the previous chapter, or indeed, as you would probably use Visual Studio to help build your applications. You can download MonoDevelop from http://monodevelop.com/Download.
  • MonoTouch: This is where the magic happens. MonoTouch is an SDK for developing applications for the iPhone using Mono. Using it, you will be able to write applications and test them on the iPhone simulator. You can download the trial version or purchase the full version from http://monotouch.net/Store.
Installing Mono

First, start by visiting the Mono project download page and downloading the Mono distribution appropriate for the platform on which you're developing. We'll be running Mono on the Mac OS X platform, as this is a requirement for developing iOS-based applications, but as you'll notice, Windows and a number of Linux and Unix distributions are also supported. Also notice that Mono conveniently separates the download options into both stable and long-term supported versions. The difference is essentially that the latest and greatest features are in the latest stable version.

NOTE: At the time of writing, the latest stable version is 2.10.5 and requires either Mac OS X Leopard (10.5), Snow Leopard (10.6) and Lion (10.7).

At the Mono downloads page, shown in Figure 3–1, select your platform and choose the SDK package appropriate for your chipset (for example, Intel or PowerPC) or choose the Universal option if you're unsure.

images

Figure 3–1. Selecting a Mono SDK package from the Downloads page

You'll notice options for both the runtime and the SDK. The runtime is used if you simply want to execute applications written for Mono, and the SDK is used if you want to execute applications and also write your own apps using the Mono API. It includes both the runtime and SDK development platform.

Once downloaded, clicking the icon in your downloads folder will start the installation, as shown in Figure 3–2.

images

Figure 3–2. Mono framework installation startup screen

Follow the on-screen instructions to complete the installation. The software is not that large, and the installation should complete pretty quickly. After it's installed, you won't see any obvious desktop or toolbar icons.

Let's test the installation. We will create a console version of the Hello, World app and use the command line and a terminal window to do this (on the Mac).

Start by creating a folder to contain your source code and create a single file named HelloWorld.cs, withthe following source code:

using System;

public class HelloWorld
{
        static public void Main ()
        {
                Console.WriteLine("Hello, World");
        }
}

This code's construct should be immediately obvious to any .NET developer. We are simply writing a text string out to the control that will display the text.

After this example is written to disk, you can attempt a compilation using the Mono compiler. While in the same directory as your source code, simply enter the following command at the command prompt:

gmcs HelloWorld.cs

If no errors are present, this will compile your source code silently, leaving HelloWorld.exe in the directory. Run this compiled executable by sending it to the Mono runtime (indicated by the mono command prefix):

mono HelloWorld.exe

This should output the text “Hello, World.”

If all of this worked, your Mono installation was successful, and we can move on to installing the MonoDevelop application. If it didn't, the mono support or community pages should help you find a resolution.

Installing MonoDevelop

Now you're ready to download MonoDevelop. Make sure that the version you're downloading matches the version of Mono you've installed. For example, MonoDevelop 2.4.2 requires at least Mono 2.4 to run.

At the home page, shown in Figure 3–3, select the version you require. Again, the choices usually include the latest stable release and the more recent, but unproven, beta release. Choose the Mac OS X platform and start the download for whichever installation mechanism you prefer (I used packages, as they are simple to get going).

images

Figure 3–3. MonoDevelop home page

After you've downloaded MonoDevelop, click the disk image. You will be prompted to complete the installation by dragging MonoDevelop to your Applications folder, as shown in Figure 3–4. This will complete your installation.

images

Figure 3–4. MonoDevelop package installation

Clicking the newly installed icon will launch the MonoDevelop application. This launches the MonoDevelop IDE, as shown in Figure 3–5.

NOTE: The installer may suggest that further MonoDevelop updates exist. If this is the case, installing them is optional, but if they are marked as stable, I recommend that you do so.

images

Figure 3–5. Running MonoDevelop

I recommend that you explore the application, taking a look at some of the links available. After you've done this, we can quickly create a console application similar to the one we used to test the Mono installation.

Start by selecting the Start a New Solution link, which will present you with a Project Template dialog box, in which you'll notice a C# Console project. Follow the on-screen instructions to create your project, selecting the location of your project and giving it a name. You'll be presented with an empty project and a default main.cs file with an implementation of Hello, World very similar to ours. Simply choose Build images Run, and a terminal window will be displayed showing the text “Hello, World.”

Now wasn't that easier!

Installing MonoTouch

Finally, to complete the “Mono trilogy,” we'll look at how to obtain and install MonoTouch. The first thing to note is that MonoTouch is not free. Depending the version you require, it can cost anything from $99 to $3,999! Consider the chart in Figure 3–6, taken from the MonoTouch web site, which details the different features and the associated prices.

images

Figure 3–6. MonoTouch version feature comparison

The key takeaway point here is that for you to use MonoTouch to create applications that are distributable via Apple's App Store, you need the Professional edition, which costs $399. A Student edition also is available, at a cost of $79. This edition allows you to create applications, but distribution onto Apple devices is possible only via ad hoc deployment—not the App Store." We'll discuss distributing your applications in Chapter 9.

For the moment, let's start with the trial version, which doesn't cost anything. Although it doesn't allow distribution via the App Store, it does permit you to test your application on the iPhone/iPad simulator. Visit http://monotouch.net/DownloadTrial, enter your email address, and click the Download button to obtain the trial version.

Start the installation by clicking the downloaded package. This will launch the installer, as shown in Figure 3–7. Follow the installation instructions. When installation is complete, the installer should take you to a web page containing the release notes for the version of MonoTouch just installed.

images

Figure 3–7. Running the MonoTouch installer

You can start to use MonoTouch immediately, but this requires that you use the command-line functions to ensure that the applications you develop are built against the MonoTouch licenses. If you prefer to use MonoDevelop, that is possible. However, you'll notice that within the version of MonoDevelop we installed, there are no such MonoTouch templates. At the time of writing, you will need to download and install a special version of MonoDevelop, updated to recognize the MonoTouch SDK. This is available from http://monodevelop.com/Download/Mac_MonoTouch. After it's downloaded and selected for installation, installing it follows a similar process to the installation of the other version of MonoDevelop you installed previously. You simply need to drag the new version onto your Applications folder, and after being prompted to replace the version you have, the new version of MonoDevelop will be installed.

Start MonoDevelop and choose Start a New Solution as before. This time, when you expand either the C# or VBNet projects, you will notice options for the iPhone and iPad projects of different types, as shown in Figure 3–8.

images

Figure 3–8. MonoDevelop with MonoTouch project templates added

Creating Hello, World Using MonoTouch

Let's build our Hello, World application. Just as you did when we used Xcode 4 in the previous chapter, start by selecting the iPhone Window-based Project and enter the name and location as you prefer. After you select Forward, a project with the default files and implementation for an iPhone Windows-based project will be created, as shown in Figure 3–9.

images

Figure 3–9. Default iPhone Windows-based application in MonoDevelop

A quick inspection will show some similarities to the Xcode project we created in Chapter 2. Both have a main.cs file with an AppDelegate instance created, making the default window key and visible. This shouldn't surprise you. It's using the Cocoa API, but with a .NET implementation.

Go ahead and build and run this application. It's not as fast as Xcode, but then again, it wouldn't be given the additional levels of abstraction. Your application will eventually launch in the iPhone simulator. Although it's a blank window (at the moment), it's your first iPhone app in .NET!

In keeping with our Xcode example, let's add a specific view and associated view controller to display our label and allow us to initialize it with the text “Hello, World.” The key difference is that in this instance, we'll being working in .NET.

Thankfully, adding a view and view controller to our project is really easy in MonoDevelop. With the project selected, select the File images New images File menu option to open the New File dialog. Expand the C# tree, and then select iPad View with Controller, as shown in Figure 3–10. Then choose New to add these files to your project, again with a default structure and implementation for you to complete.

images

Figure 3–10. MonoDevelop New File dialog

As you did in the Xcode example, select the HellowWorldView.xib file. This will launch Interface Builder. Continue to add a label and hook it up as an outlet, as per the Xcode 4 example in the previous chapter.

Now we can set the text value. Once our code is hooked up, we can simply override the ViewDidLoad() method of the ViewController class, and within this, access the label user object by name, like so:

public override void ViewDidLoad ()
{
        base.ViewDidLoad();
        lbl.Text = @"Hello, World";
}

If you now run the application, your MonoTouch-based iPhone app will behave exactly the same as your Xcode 4 iPhone app, and display “Hello, World” on the device.

We've taken a look at how to install and create applications—including our now infamous Hello, World application—running on an iPhone (well, the simulator) using MonoTouch. But there are many more options available. In the rest of the chapter, we'll concentrate on how to create the same test application with a couple of the more stable and popular options available, using languages other than Objective-C and .NET that might be familiar to you, and in some cases, even running on a Microsoft Windows-based PC.

Using Appcelerator's Titanium Mobile

The Appcelerator Titanium Mobile platform was created to support the cross-platform development of native applications for mobile, tablet, and desktop devices using languages that are perhaps more common and leveraging the JavaScript-based API. The Titanium Mobile SDK works with the native SDK toolchains (in Apple's case, the iOS SDK) to combine your JavaScript source code, a JavaScript interpreter, and your static assets into an application executable that can be installed on an emulator or mobile device.

Note that, due to Apple's licensing agreement, Titanium Mobile has restrictions on which platforms can be used to target which mobile devices and operating systems. Table 3-1 summarizes the support options.

images

Despite these restrictions, it is worth noting that it's free to get started, and we'll be focusing on the Mac OS X operating system. As with our MonoTouch example, we'll install Titanium Mobile and then use it to create our Hello, World iPhone application. The difference is that this time, we'll be writing our code in JavaScript.

Installing Titanium

The first step is to download the required software from the Appcelerator's Titanium home page at http://www.appcelerator.com/products/download/. After it's downloaded, install the application in the traditional Mac manner by dragging the Titanium Developer icon onto your Applications folder, which will invoke its installation. Follow the on-screen instructions provided by the installer, as shown in Figure 3–11.

NOTE: Titanium Developer is the overarching GUI, which sits across the various SDKs that exist and acts as your single reference point. Once Titanium Developer is installed, the different mobile SDKs are installed within it. The first time it is run, it will automatically attempt to download the current versions of the Mobile and Desktop SDKs.

images

Figure 3–11. Titanium Developer installer

After you've installed Titanium, it is also worth registering as a Titanium developer, as this will grant you access to online resources to help you when developing your application.

Creating Hello, World Using Titanium

Start up the Titanium Desktop and allow it to update the Mobile SDK. Then you'll be presented with an empty GUI. The iOS SDK was automatically detected by the installation, so there was no need to install that package (unless you want to use a more current version). Let's get cracking.

Start by choosing the New Project option. This will present you with a dialog with some standard project properties to fill in, as shown in Figure 3–12. When completing the on-screen form, you may notice that under the Project Type drop-down list, the iPhone option uses the Mobile project type, whereas there is a specific option for the iPad. Once you've completed the form, choose the Create Project option to create the Mobile application.

images

Figure 3–12. Titanium Mobile New Project dialog

NOTE: Notice that on my installation, I chose not to install the Android SDK. If you want to give that a try, the online resources provide you with instructions on how to do so.

The blank HelloWorld project will be shown, with options to visit the Dashboard, Edit, or Test & Package your application. On the Dashboard, you'll see a number of online resources to help you get started, asshown in Figure 3–13.

images

Figure 3–13. Empty project Dashboard in Titanium Mobile

If you jump straight to the Test & Package option tab, you'll see the screen shown in Figure 3–14. One of the options is the Launch button, which will build and run your application within the mobile device simulator matching the project type chosen, which is the iPhone simulator in our example.

images

Figure 3–14. Test & Package tab in Titanium Mobile

The default application generated by Titanium Mobile provides two tabs with a text label on each tab. We will modify this to show only one tab with our chosen text (“Hello, World”) on it.

First, let's take a look at the folder structure and files created. In addition to the files created, the following folders were added:

  • uild: This folder contains the files imported from the SDK and automatically generated files that typically are not modified, such as the main.m file. The binaries and libraries are also located here.
  • esources: This folder contains the resources that support your application, including source code. Here, you will find the app.js file, the main JavaScript application file used to start your application. We will make our modifications in this file.

Take a little time to examine the app.js file. It's pretty straightforward. As noted, the default implementation is to create two tabs, each containing a label, and add these to the main application window, and we're going to adjust this to display just the one tab and label.

Let's examine some of the highlights of the code. First, the most obvious thing to note is that an initial reference is made to the Titanium.UI namespace. This namespace contains the classes that provide access to the user-interface objects. It uses this information to initially set the background and also to create a tab group, as follows:

var tabGroup = Titanium.UI.createTabGroup();

The new Tab objects will be added to this group, with each Tab pointing to a window that will be displayed when chosen (although our code will implement only a single tab to show the “Hello, World” text). Consider the following code:

//
// create base UI tab and root window
//
var win1 = Titanium.UI.createWindow({  
    title:'Tab 1',
    backgroundColor:'#fff'
});
var tab1 = Titanium.UI.createTab({  
    icon:'KS_nav_views.png',
    title:'Tab 1',
    window:win1
});

This creates the main window and assigns it the caption of Tab 1, using a Tab object to which this window is assigned. When selected in the application, this tab will display this window.

Let's skip to the code that creates the new label, after creating a new window and assigning this to the new tab. Consider the following segment:

var label1 = Titanium.UI.createLabel({
        color:'#999',
        text:'Hello, World',
        font:{fontSize:20,fontFamily:'Helvetica Neue'},
        textAlign:'center',
        width:'auto'
});

win1.add(label1);

We simply create a label object and assign it the “Hello, World” text. We can then add this to the main window, referenced by the win1 variable.

Finally, we add our tab to the tab group and open the tab group, displaying the window and our tabs on the main application.

//
//  add tabs
//
tabGroup.addTab(tab1);


// open tab group
tabGroup.open();

In carrying out these actions, the application will also display our “Hello, World” text. And since it's built within the Titanium Developer IDE, it will also launch the iPhone simulator, as shown in our running application in Figure 3–15.

images

Figure 3–15. Titanium Mobile HelloWorld application

As you can see, the Titanium IDE is easy to work with and has a complete API. I'll leave further discovery for you to try out with the help of the online reference material and the Kitchen Sink application, which provides out-of-the-box examples to examine and modify.

Using the Marmalade SDK

The Marmalade SDK has the same goal as Titanium Mobile, which is to enable you to build your application against an API that is agnostic of the mobile device and its operating system. With it, you can develop cross-platform applications in a single hit!

The Marmalade SDK supports single-click deployment to a number of operating systems, including iOS, Android, Symbian, Windows Mobile, and more. We'll be using the Xcode IDE to create our Marmalade-based application.

NOTE: While the Marmalade API supports core features for all types of applications, it has a bias toward games development. You will find a rich set of APIs that enable you to write compelling mobile games.

The Marmalade SDK can be thought of as the following two components:

  • MarmaladeSystem: This provides the operating system abstraction API, which when used with the associated libraries, allows you to build mobile applications for a variety of operating systems. It provides a suite of tools and the C API called the S3E API.
  • MarmaladeStudio: This provides a suite of tools and runtime components to allow a focused development on high-performance 2D and 3D graphics and animations.

Installing Marmalade

To start, you will need to register at the Marmalade SDK web site (http://www.madewithmarmalade.com/downloads). Once registration is complete, you will be sent an activation email message. After you've activated your account, you'll be able to download an evaluation copy of the software to try out. (Of course, you could purchase a copy, but I'm assuming you want to try it out first.) The evaluation version allows you to do everything apart from publicly distribute the applications you have written.

After you've downloaded the package, start the installation by selecting the icon. You will be presented with the installer, as shown in Figure 3–16.

images

Figure 3–16. Marmalade installer

Follow the on-screen instructions, including entering your Marmalade account credentials in order to install under the 30-day evaluation license, which you obtained previously on the web site.

The default installation location is /Developer/Marmalade-SDK/<version>, where <version> is the version of the SDK you installed. After installation, you won't see any new icons, but you will find new files installed in the target location. The following are the key folders installed:

  • Applications: This folder contains the SDK's top-level applications.
  • docs: This folder contains documentation, including user manuals.
  • examples: This folder contains example applications.
  • extensions: This folder contains the Extensions Development Kit.
  • Marmalade: This folder contains additional tools used by Marmalade.
  • s3e: This folder contains the Marmalade System header files and runtime libraries.
  • modules: This folder contains all Marmalade Studio header files and runtime libraries.

Creating Hello, World Using Marmalade

Now that you've installed Marmalade, and perhaps had a look around its folders and the files, let's create our application.

Start by firing up Xcode (we could, of course, use the command line, but IDEs were invented to make our lives easier, so let's use them). We will use one of the examples provided, as they supply all the configuration necessary for Xcode 4 to deploy to the Marmalade simulator. This simulator is the tool used to debug native Marmalade executable files (with an .s3e extension).

The .s3e files are essentially dynamic-link libraries that are platform-agnostic. The Marmalade SDK allows you to build the same application for a number of different platforms, simply by reconfiguring the IDE. For the moment, we'll stick with deploying it to the Marmalade simulator, as we're just interested in how you might get started with such a feature.

Using the Finder application, locate the Hello, World example, which is under exampless3es3eHelloWord. In this folder, you will see the s3dHelloWorld.mkb file, which is essentially the project file. Open it by choosing Open With from the menu and selecting the Mkb application provided within the Marmalade SDK (in the Applications folder), as shown in Figure 3–17.

images

Figure 3–17. Opening a Marmalade SDK project using Mkb

This will launch Xcode, from which can choose the Debug Simulator option scheme from the top of the window, and then choose Build. Next, choose Run. This executes the application within the simulator, displaying our “Hello, World” text, as shown in Figure 3–18. From here, you can use the Marmalade SDK to target different platforms, including the iOS, to develop your apps.

images

Figure 3–18. Marmalade simulator running our application

Summary

This chapter built on the knowledge you have gained about using native tools in previous chapters by looking at the available third-party options. One of the most popular alternatives is Mono and MonoTouch, providing a .NET implementation of the Cocoa and Cocoa Touch APIs, but in the .NET language. This option provides you free and native-feeling access to iPhone and iPad development resources, complete with a Visual Studio-like IDE in the form of MonoDevelop.

As you learned, MonoTouch is not the only option, provided that you're happy to use languages other than .NET, such as JavaScript or C/C++. Other options available to you include Titanium Mobile and the Marmalade SDK. Each of these options allows you to write native applications, not only for the iPhone and iPad, but also for other devices. In fact, these platforms are built specifically to target cross-platform development, allowing you to develop against a single API but build applications that can target numerous mobile devices with different operating systems, including Apple, Android, and Symbian.

As useful as these third-party options are, apart from MonoTouch, the lack of support for the .NET language makes it more difficult to make the transition and reuse your experience. Clearly, the use of MonoTouch makes this far easier, but you will be dependent on the support MonoTouch has for the iOS and Cocoa Touch SDKs. If support is not provided, or features are incomplete, you will not be able to use this option—unless you write your own implementation!

So, apart from contributing toward the Mono project yourself, the other option is to map your C# and .NET Framework knowledge onto the iOS SDK and use Objective-C. The rest of the book will focus on doing just that and bridging the gap in your knowledge.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset