Friday, April 30, 2010

The iPhone OS Architecture - Part 1 (The very basics)

iPhone OS is really amazing! So, I think it's a good idea for us to start studying how iPhone OS works. iPhone OS applications are mostly based on Objective-C programming, which is part of Cocoa Touch architecture. Cocoa Touch? Below, let's see an introduction overview of this Apple architecture for the iPhone.

When I started programming applications for the iPhone, I particularly found it completely different from everything I already coded within my 15 years of coding experience. At the beginning, I admit it was weird... but after studying and acquiring some experience on that, I really found it amazing.

The curious thing is that it's based on NeXTStep Objective-C, from 1982! It's now ressurrected for the iPhone.

First of all, you use Xcode and Interface Builder in parallel. Cocoa Touch is based on MVC (Model - View - Controller) architecture. I mean, you code the Controller in Objective-C and, almost at the same time, use Interface Builder to create the UIKit visual objects and link them to your visual object events into methods, instance variables (outlets) you coded for the Controller. It's a different concept and that's how it works on Cocoa/Cocoa Touch. We are used to create a visual object, click twice on it, and then code its event. Cocoa works different at this point.

Let's go through the brief overview of iPhone OS... First, let's talk about Mac OS X, which was rewritten and recoded entirely in Cocoa and Objective-C. By the way, according to Apple, the Snow Leopard version (10.6) was completely rewritten from its antecessor Leopard (10.5) and the code in 10.6 was reduced to less than a half of previous 10.5 [1]. For the iPhone OS, Apple introduced almost all same benefits they did for the Snow Leopard and this really allows the iPhone OS to run faster and support complex applications, as well. Here is iPhone OS architecture:



The iPhone OS architecture is divided into four important layers [2]:

  • Core OS: includes OS X Kernel, Power Management, Mach 3.0, Keychain, BSD, Certificates, Sockets, File System, Security and Bonjour
  • Core Services: includes Collections, Core Location, Address Book, Net Services, Networking, Threading, File Access, OS Preferences, SQLite and URL utilities
  • Media: includes Core Audio, JPG/PNG/TIFF engines, OpenAL, PDF engine, Audio Mixing, Quartz (2D), Audio Recording, Core Animation, Video Playback and OpenGL ES
  • Cocoa Touch: includes Multi-Touch Events, Alerts, Multi-Touch Controls, Web Views, Accelerometer, People Picker, View Hierarchy, Image Picker, Localization, Controllers

Based on above architecture, we will code in the Cocoa Touch layer in order to access all other layers. For example, if you want your iPhone application to detect the end user GPS coordinates, you will add the Xcode Core Location Framework into your Xcode project. So, you will have Cocoa Touch layer accessing Core Services layer. And so on.

Bibliography:

[1] APPLE, Inc. Mac OS X: Enhancements and Refinements. http://www.apple.com/macosx/refinements/enhancements-refinements.html#installation

[2] APPLE, Inc. iPhone OS Overview. http://developer.apple.com/iphone/library/referencelibrary/GettingStarted/URL_iPhone_OS_Overview