Get Start

What is iOS?

iOS is the world’s most advanced mobile operating system, continually redefining what people can do with a mobile device. Together, the iOS SDK and Xcode IDE make it easy for developers to create revolutionary mobile apps.

Versions of iOS

At the time of writing, iOS is in its sixth revision — that is, version 6.0. Its major versions are as follows:

1.0 - Initial release of the iPhone
1.1 - Additional features and bug fixes for 1.0
2.0 - Released with iPhone 3G; comes with App Store
2.1 - Additional features and bug fixes for 2.0
2.2 - Additional features and bug fixes for 2.1
3.0 - Third major release of the iPhone OS
3.1 - Additional features and bug fixes for 3.0
3.2 - This version release is for the iPad only.
4.0 - Fourth major release of the iPhone OS. Renamed as iOS. This version is designed for the new iPhone 4 and it also supports older devices, such as the iPod touch and iPhones.
5.0 - Fifth major release of the iOS. Supports new features like iCloud, iMessage, Twitter integration, Notification Center, etc.
6.0 – Sixth major release of the iOS.

Obtaining the Tools and SDK

To develop for iOS, you need to download the iOS SDK. The iOS SDK comes with free Xcode from the Mac App Store.

Before you download and install Xcode, make sure you satisfy the following system requirements:
Only Intel Macs are supported, so if you have another processor type (such as the older G4 or G5 Macs), you’re out of luck.
Your system is updated with the latest Mac OS X Lion release.
An actual iPhone/iPod touch/iPad is highly recommended, although not strictly necessary. To test your application, you can use the included iOS Simulator (which enables you to simulate an iPhone or an iPad). However, to test certain hardware features like the accelerometer and gyroscope, you need to use a real device.
When Xcode is downloaded, proceed with installing it. Accept a few licensing agreements and then select the destination folder in which to install the SDK.

Components of Xcode

The Xcode package includes a suite of development tools to help you create applications for your iPhone, iPod touch, and iPad. It includes the following:
Xcode IDE — Integrated development environment (IDE) that enables you to manage, edit, and debug your projects
Dashcode — Integrated development environment (IDE) that enables you to develop web-based iPhone and iPad applications and Dashboard widgets. Dashcode is beyond the scope of this book.
iOS Simulator — Provides a software simulator to simulate an iPhone or an iPad on your Mac
Interface Builder — Visual editor for designing user interfaces for your iPhone and iPad applications
Instruments — Analysis tool to help you both optimize your application and monitor for memory leaks in real time.

Xcode

To launch Xcode, double-click the Xcode icon located in the /Developer/Applications folder. Alternatively, go the quicker route and use Spotlight: Simply type Xcode into the search box and Xcode should be in the Top Hit position.



Using Xcode, you can develop different types of iPhone, iPad, and Mac OS X applications using the various project templates below.



For iOS applications, each template gives you the option to select the platform you are targeting - iPhone, iPad, or Universal (runs on both iPhone and iPad).

The IDE in Xcode provides many tools and features that make your development life much easier. One such feature is Code Sense, which displays a popup list showing the available classes and members, such as methods, properties, and so on.

iOS Simulator

The iOS Simulator is a very useful tool that you can use to test your application without using your actual iPhone/iPod touch/iPad. The iOS Simulator is located in the /Developer/Platforms/iPhoneSimulator.platform/Developer/Applications folder. Most of the time, you don’t need to launch the iOS Simulator directly — running (or debugging) your application in Xcode automatically brings up the iOS Simulator. Xcode installs the application on the iOS Simulator automatically.


The iOS Simulator can simulate different versions of the iOS. This capability is useful if you need to support older versions of the platform, as well as test and debug errors reported in the application on specific versions of the OS.



THE IOS SIMULATOR IS NOT AN EMULATOR

To understand the difference between a simulator and an emulator, keep in mind that a simulator tries to mimic the behavior of a real device. In the case of the iOS Simulator, it simulates the real behavior of an actual iPhone/iPad device. However, the Simulator itself uses the various libraries installed on the Mac (such as QuickTime) to perform its rendering so that the effect looks the same as an actual iPhone. In addition, applications tested on the Simulator are compiled into x86 code, which is the byte-code understood by the Simulator. A real iPhone device, conversely, uses ARM-based code.

In contrast, an emulator emulates the working of a real device. Applications tested on an emulator are compiled into the actual byte-code used by the real device. The emulator executes the application by translating the byte-code into a form that can be executed by the host computer running the emulator.

To understand the subtle difference between simulation and emulation, imagine you are trying to convince a child that playing with knives is dangerous. To simulate this, you pretend to cut yourself with a knife and groan in pain. To emulate this, you actually cut yourself.


The iOS Simulator can simulate different versions of the iOS. This capability is useful if you need to support older versions of the platform, as well as test and debug errors reported in the application on specific versions of the OS.


In addition, the iOS Simulator can simulate different devices — iPad, iPhone (3G and 3GS), and iPhone 4 with Retina display.

Features of the iOS Simulator

The iOS Simulator simulates various features of a real iPhone, iPod touch, or iPad device. Features you can test on the iOS Simulator include the following:

Screen rotation - left, right, top, and upside down
Support for gestures:
Tap
Touch and Hold
Double-tap
Swipe
Rotate
Drag
Pinch
Low-memory warning simulations

However, the iOS Simulator, being a software simulator for the real device, does have its limitations.

The following features are not available on the iOS Simulator:
Making phone calls
Accessing the accelerometer
Sending and receiving SMS messages
Installing applications from the App Store
Camera
Microphone
Several features of OpenGL ES

Note also that the speed of the iOS Simulator is more tightly coupled to the performance of your Mac than the actual device. Therefore, it is important that you test your application on a real device, rather than rely exclusively on the iOS Simulator for testing.

Despite the iOS Simulator’s limitations, it is definitely a useful tool for testing your applications. That said, testing your application on a real device is imperative before you deploy it on the App Store.



Uninstalling Applications from the iOS Simulator

The user domain of the iOS file system for the iOS Simulator is stored in the ~/Library/Application Support/iPhone Simulator/ folder.

All third-party applications are stored in the <iPhoneUserDomain>/<version_no>/Applications/
folder. When an application is deployed onto the iOS Simulator, an icon is created on the Home screen and a file and a few folders are created within the Applications folder.

To uninstall (delete) an application, execute the following steps:

1. Click and hold the icon of the application on the Home screen until all the icons start wriggling. Note that all the icons now have an X button displayed on their top-left corner.
2. Click the X button next to the icon of the application you want to uninstall.
3. An alert window appears asking if you are sure you want to delete the icon. Click Delete to confirm the deletion.

The easiest way to reset the iOS Simulator to its original state is to select iOS Simulator ➪ Reset Content and Settings . . . .

Interface Builder

Interface Builder is a visual tool that enables you to design the user interfaces for your iPhone/iPad applications. Using Interface Builder, you drag and drop views onto windows and then connect the various views with outlets and actions so that they can programmatically interact with your code.


Instruments

The Instruments application enables you to dynamically trace and profile the performance of your Mac OS X, iPhone, and iPad applications.


Using Instruments, you can do all of the following:
Stress test your applications.
Monitor your applications for memory leaks.
Gain a deep understanding of the executing behavior of your applications.
Track diffi cult-to-reproduce problems in your applications.

Architecture of the IOS

Although this book doesn’t explore the innards of iOS, understanding some of its important characteristics is useful.Below figer shows the different abstraction layers that make up the Mac OS X and iOS (which is used by the iPhone, iPod touch, and iPad).


The bottom layer is the Core OS, which is the foundation of the operating system. It is in charge of memory management, the file system, networking, and other OS tasks, and it interacts directly with the hardware. The Core OS layer consists of components such as the following:

OS X Kernel
Mach 3.0
BSD
Sockets
Security
Power Management
Keychain
Certifi cates
File System
Bonjour

The Core Services layer provides an abstraction over the services provided in the Core OS layer. It provides fundamental access to iOS services and consists of the following components:

Collections
Address Book
Networking
File Access
SQLite
Core Location
Net Services
Threading
Preferences
URL Utilities

The Media layer provides multimedia services that you can use in your iPhone and iPad applications. It consists of the following components:

Core Audio
OpenGL
Audio Mixing
Audio Recording
Video Playback
JPG, PNG, TIFF
PDF
Quartz
Core Animation
OpenGL ES

The Cocoa Touch layer provides an abstraction layer to expose the various libraries for programming the iPhone and iPad, such as the following:

Multi-Touch events
Multi-Touch controls
Accelerometer
View Hierarchy
Localization
Alerts
Web Views 
People Picker
Image Picker
Controllers

In iOS programming, all the functionalities in each layer are exposed through various frameworks that you will use in your project. Subsequent chapters in this book demonstrate how to use these frameworks in your projects.

Testing on Real Devices

One of the most common complaints about developing applications for the iPhone and iPad is how difficult Apple makes it to test a new application on an actual device. Nonetheless, for security reasons, Apple requires all applications to be signed with a valid certificate; and for testing purposes, a developer certificate is required.

To test your applications on a device, you must sign up for the iOS Developer Program and request that a developer certificate be installed onto your device. 

Screen Resolutions

The iPhone 4S is a beautiful device with a high-resolution screen. At 3.5 inches (diagonally), the iPhone screen supports muti-touch operation and allows a pixel resolution of 960 × 640 at 326 ppi. When designing your application, note that because of the status bar, the actual resolution is generally limited to 920 × 640 pixels. Of course, you can turn off the status bar programmatically to gain access to the full 960 × 640 resolution.

Also, be mindful that users may rotate the device to display your application in landscape mode. You need to make provisions to your user interface so that applications can still work properly in landscape mode. The older iPhones (iPhone 3G/3GS) and the iPod touch have lower resolutions compared to the iPhone 4/4S. 

They have a resolution of 480 × 320 pixels, one quarter of the resolution of the iPhone 4. When programming for the iPhones, it is important to note the difference between points and pixels. For example, the following statement specifies a frame that starts from the point (20,10) with a width of 280 points and a height of 50 points: 

CGRect frame = CGRectMake(20, 10, 280, 50);

On the older iPhones, a point corresponds to a pixel. Thus, the preceding statement translates directly to the pixel (20,10), with a width of 280 pixels and a height of 50 pixels. However, if the statement is executed
within the iPhone 4/4S, a point translates to two pixels. Thus, the preceding statement translates into the pixel (40,20), with a width of 560 pixels and a height of 100 pixels. The translation is performed automatically by the OS, which is very useful because it enables older applications to run and scale correctly without modifications on the iPhone 4/4S. The iPad has a pixel resolution of 1,024 × 768 at 132 ppi.


Platform Resolutions


Hello World Application in iOS

Power up Xcode and you should see the Welcome screen,



To create a new iPhone project, click the Create a new Xcode project button (or choose File ➪ New  New Project). Below figure shows the different types of projects you can create using Xcode. The left panel shows the two primary categories - iPhone OS and Mac OS X. The iPhone uses the iOS, so click the Application item listed under iOS to view the different templates available for developing your iPhone application.


Although you can create quite a few types of iPhone applications, for this chapter select the Single View Application template and then click Next. Name the project HelloWorld and provide a company identifier for your application. Name the Class Prefix to be the same as the project name and select iPhone as the Device Family. Finally, ensure that all the options are unchecked and then click Next. You will be asked to select a folder in which to save your project. Xcode then proceeds to create the project for the template you have selected. Below figure shows the various files and folders automatically created for your project.

The left panel of Xcode shows the groups in the project. You can expand each group or folder to reveal the files contained in it. To edit a particular file, select it from the list, and the editor on the right panel opens the file for editing. If you want a separate window for editing, simply double-click the file to edit it in a new window.





Using Interface Builder

At this point, the project has no UI. To prove this, simply press Command-R (or select Product ➪ Run), and your application is deployed to the included iPhone Simulator. Below figure shows the blank screen displayed on the iPhone Simulator. Note how it looks now, because as you go through the chapter you will see changes occur based on your actions.



Obviously, a blank screen is not very useful. Therefore, it’s time to try adding some views to your application’s UI. In the list of files in your project, you’ll notice a file with the .xib extension - HelloWorldViewController.xib

Files with .xib extensions are basically XML files containing the UI definitions of an application. You can edit .xib files by either modifying their XML content
or, more easily (and more sanely), using Interface Builder.

Interface Builder, integrated into Xcode (prior to Xcode 4, Interface Builder is a separate application that ships with the iOS SDK), enables you to build the UI of iPhone (and Mac) applications by using drag and drop.

Select the HelloWorldViewController.xib file to edit it using Interface Builder. Figure below shows Interface Builder displaying the content of HelloWorldViewController.xib.





In the Utilities area on the right, go to the Object Library section and scroll down to the Label view and drag and drop a Label onto the View window.


After the Label is added, select it and go to the Attributes Inspector window (you can view this by choosing View Utilities Show Attributes Inspector). Enter Hello World! in the Text field. Then, in the Alignment field, click the center alignment button.




With the Label still selected, click on the “T” icon displayed next to the Font field and select the Helvetica Custom font. Set the font size to 36. Resize the Label so that it now looks like below figure. 

Next, from the Library window, drag and drop a Text Field to the View window, followed by a Round Rect Button. Modify the attribute of the Round Rect Button by entering Click Me! in the Title field of its Attributes Inspector window. Figure 2-11 shows how the View window looks now.




Run the application again by pressing Command-R. The iPhone Simulator now displays the modified UI. Click the Text Field and watch the keyboard automatically appear. Click the Home button on the iPhone Simulator, and you will see that your application has been installed on the Simulator. To go back to the application, simply click the HelloWorld icon.



  







No comments:

Post a Comment