Developing with Objective Pascal

Part 3: iOS Simulator and Xcode 4


Contents

Introduction
Requirements
Parsing iOS headers
Your first Objective Pascal Cocoa Touch app
Adding UI elements and delegate code
Next steps


Introduction

These notes describe how to get started developing Cocoa Touch applications for iOS using Pascal. You should already have installed Xcode 4 and reviewed Part 1 and Part 2.

As with OS X development, iOS development is done in Apple's Xcode 4:

  1. You create a new project in Xcode using a custom Pascal template instead of a built-in Objective C template.

  2. You compile your Pascal code in Xcode using a Free Pascal compiler, which provides an "Objective Pascal" dialect for working with Cocoa Touch.

To avoid information overload, this article focuses on creating iOS apps for the iOS Simulator (included with Xcode). Part 4 turns to the subject of compiling for an actual iOS ARM device. Note that if you do not have access to an iOS device, you can skip Part 4 for now and just use the iOS Simulator for all development.

What is Cocoa Touch?

Cocoa Touch is a group of frameworks included on every iOS device that you use to create your app's user interface. Key among these frameworks are UIKit, which is analogous to Cocoa's AppKit, and the familiar Foundation. You can think of UIKit as an AppKit that has been reorganized, modernized, streamlined and optimized for touch devices. It consists of many fewer classes than Cocoa, yet adds a few of its own. For example, where WebView is in the separate WebKit framework on OS X, on iOS UIWebView is part of the UIKit framework.

A list of UIKit classes is here and a list of Foundation classes is here. An overview of other Cocoa Touch frameworks is here and an overview of iOS is here.

How does Cocoa Touch development differ from Cocoa?

Cocoa Touch resembles Cocoa in many ways and many of the concepts and techniques that you learn while developing for Cocoa apply to Cocoa Touch development as well. However, there are a few differences worth noting.

Requirements


Parsing iOS headers

The iOS header files (.h) are ready to use if you're developing with Objective C, but not with Objective Pascal. Even though Objective Pascal can work with ObjC classes, it needs its own Pascal equivalents to the ObjC header files. Since Apple does not permit derivatives of the iOS SDK files to be distributed, you'll need to create these Pascal files yourself.

Fortunately, Ryan Joseph (thealchemistguild.com) and Jonas Maebe (Objective Pascal's architect) have teamed up and created a parser that can parse the header files from just about any iOS (or OS X) framework and create these Pascal files automatically.

The latest version of the parser is available here:

iOS_6_Parsing_Status.html

Follow the parsing instructions, then compile the iPhoneAll unit for the Simulator.

The iPhoneAll unit is analogous to the CocoaAll unit used in Parts 1 and 2 and provides access to the UIKit, Foundation, QuartzCore and OpenGLES frameworks. Since the iPhoneAll unit was not installed with the compiler, the compiler doesn't know where it is, so you'll need to specify the path to it when you compile an iOS app. And to avoid recompiling iPhoneAll each time you switch between the Simulator and ARM compilers, you'll probably want to move the compiled files somewhere else. The template used in the next section assumes that the compiled iPhoneAll files for the Simulator are in the following location, although you can place them wherever you want and just edit the path in the build settings of any project you create with the template:

/Developer/ObjectivePascal/units/i386-iphonesim
Here are the files you need to copy:

iPhoneAll.o
iPhoneAll.ppu
AnonClassDefinitionsUikit.o
AnonClassDefinitionsUikit.ppu

Your first Objective Pascal Cocoa Touch app

Start Xcode and choose File | New | New Project. Under iOS you should see Objective Pascal (if not, you probably don't have the templates installed in the correct location). Click Objective Pascal and select the Window-Based Application template, then click Next and enter the following:

Now click Next and navigate to where you want to save the files that will be created. For example, if you navigate to /Users/yourname/Documents and you entered Test3 for your project name, Xcode will create a folder named Test3 under Documents. Inside this folder it will create two additional folders, named Test3 (for your app's files) and Test3.xcodeproj (for its project files).

Several of the files that the template creates are the same as in Part 1, but there are some differences. For example, to save on vertical space in the Project navigator with a universal app, the template dispenses with grouping the Objective C files and the Pascal files together. Instead, files are grouped by device. Other differences are noted below.

   Figure 3-1. A universal app's files.

ReadMe.txt - brief getting-started instructions for the new project.

Test3AppDelegate.* files - similar to Part 1.

Test3AppDelegate_iPhone.* files - subclass of Test3AppDelegate for iPhone; .xib file loaded when run on an iPhone.

Test3AppDelegate_iPad.* files - subclass of Test3AppDelegate for iPad; .xib file loaded when run on an iPad.

Supporting Files - similar to what's found in Parts 1 and 2; the Pascal program file is in this group too.

Frameworks - all Cocoa Touch apps use the UIKit and Foundation frameworks; you can expand these to view the framework header files (.h).


On the target's Summary tab, set the iOS Deployment Target to the earliest version of iOS you want to support. Once you've done this, go ahead and compile and run your app in the iOS Simulator (Product | Run), although the app doesn't really do anything yet.


Adding UI elements and delegate code

Adding UI controls and code to a Cocoa Touch window-based app for the iPhone or iPad is similar to adding them to the simple Cocoa app described in Part 1, so we're skipping that exercise and moving right to the universal Cocoa Touch app. With a universal app, there are two window .xib files. Since an iPad's screen has over five times the area of an iPhone (or iPod touch) screen, you'll want to design two different user interfaces, one for the iPad screen and one for the iPhone screen.

Set up Xcode's panes as described in Part 1, then do this:

Now choose Product | Build and build your app. If the build fails, check the error message and review the steps above. Note what we're doing here: since both windows have a button that we want to respond to in the same way (by simply changing the button's label), we only have to do this in the Test3AppDelegate superclass, not in each subclass. The subclasses (Test3AppDelegate_iPhone and Test3AppDelegate_iPad) are where you make connections for UI controls that are specific to the iPhone or iPad.

Once the app builds successfully, run it in the iOS Simulator. Make sure iPhone Simulator is selected as the Scheme just above the Project navigator. When the app runs (in a likeness of an iPhone), click the button - its label should change to "Touched!".

Now select iPad Simulator as the Scheme and run the app again. You should now see the likeness of an iPad. Again, click the button - its label should also change.


Next steps

You'll notice that there's also an iOS Device choice in the Scheme list. If you select this and choose Product | Build, you might get an error message about a missing provisioning profile. That's for Part 4 - setting up to run on an actual iOS device.


Copyright 2011 by Phil Hess.

macpgmr (at) fastermac (dot) net

First posted April 24, 2011; last edited Feb. 25, 2013.