In Hour 1,
you learned that you don’t even need an iPhone to start developing for
the platform. The reason for this is the iPhone Simulator included with
the Apple developer tools. The iPhone Simulator does a great job of
simulating the Apple iPhone, with the Safari, Contacts, Settings, and
Photos apps available for integration testing, as shown in Figure 1.
Figure 1. The iPhone Simulator includes a stripped-down version of the iPhone apps.
Targeting the simulator for
the early stages of your development can save you a great deal of time;
you won’t need to wait for apps to be installed on your physical device
before seeing the effects of changes in your code. In addition, you
don’t need to buy and install a developer certificate to run code in
The simulator, however, is not a perfect
iPhone. It can’t display OpenGL graphics, simulate complex multitouch
events, or provide readings from some of the iPhone sensors (GPS,
accelerometer, and so on). The closest it comes on these counts is the
ability to rotate to test landscape interfaces and a simple “shake”
motion simulation. That said, for most apps, it has enough features to
be a valuable part of your development process.
One thing that you absolutely cannot
count on in the simulator is that your simulated app performance will
resemble your real app performance. The simulator tends to run silky
smooth, whereas real apps might have more limited resources and not
behave as nicely. Be sure to occasionally test on a physical device so
that you know your expectations are in line with reality.
Launching Applications in the Simulator
To launch an application in
the simulator, open the project in Xcode, make sure that the active SDK
is set to iPhone Simulator, and then click Build and Run. After a few
seconds, the simulator will launch and the application will be
displayed. You can test this using the HelloSimulator project included
in this hour’s Projects folder.
Once up and running, the HelloSimulator app should display a simple line of text (see Figure 2).
Figure 2. Click Build and Run in Xcode to launch and run your application in the Simulator.
When an application is
running, you can interact with it using your mouse as if it were your
fingertip. Click buttons, drag sliders, and so on. If you click into a
field where input is expected, the iPhone keyboard will display. You
can “type” using your Mac keyboard or by clicking the keyboard’s
buttons onscreen. The iPhone’s copy and paste services are also
simulated by clicking and holding on text until the familiar loupe
Clicking the virtual Home button (or choosing Hardware, Home from the menu) exits the application.
Did You Know?
Launching an application in the
simulator installs it in the simulator, just like installing an app on
the iPhone. When you exit the app, it will still be present on the
simulator until you manually delete it.
To remove an
installed application from the simulator, click and hold the icon until
it starts “wiggling,” and then click the X that appears in the
upper-left corner. In other words, remove apps from the simulator in
the exact same way as you would remove them from a physical iPhone!
To quickly reset the simulator back to a clean slate, choose Reset Content and Settings from the iPhone Simulator menu.
Generating Multitouch Events
Did You Know?
By default, your
application will be displayed on a simulated iPhone 3GS screen. To
switch to an iPhone 4 display, choose Hardware, iPhone 4 from the
Simulator’s application menu.
Even though you have only
a single mouse, you can simulate simple multitouch events, such as
two-finger pulls and pinches, by holding down Option when your cursor
is over the iPhone Simulator “screen.” Two circles, representing
fingertips, will be drawn and can be controlled with the mouse. To
simulate a touch event, click and drag while continuing to hold down
Option. Figure 3 shows the “pinch” gesture.
Figure 3. Simulate simple multitouch with the Option key.
Try this using the
HelloSimulator app. You should be able to use the simulator’s
multitouch capabilities to shrink or expand the onscreen text and image.
Rotating the Simulated iPhone
To simulate a rotation on the iPhone, choose Rotate Right or Rotate Left from the Hardware menu (see Figure 4). You can use this to rotate the simulator window through all four possible orientations and view the results onscreen.
Figure 4. Rotate the interface through the possible orientations.
Again, test this with HelloSimulator. The app will react to the rotation events and orient the text properly.
Simulating Other Conditions
You will want to test against
a few other esoteric conditions in the simulator. Using the Hardware
menu, you can access these additional features:
Device: Choose from the iPhone, iPhone 4, and iPad devices to simulate your application on each.
Check to see how your app will behave on earlier versions of the iOS.
This option enables you to choose from many of the recent versions of
Shake Gesture: Simulate a quick shake of the iPhone.
Simulates the condition of a locked iPhone. Because a user can lock an
iPhone while an application is running, some developers choose to have
their programs react uniquely to this situation.
Simulate Memory Warning:
Triggers an application’s low-memory event. Useful for testing to make
sure your application exits gracefully if resources run low.
Toggle In-Call Status Bar:
When a call is active and an application is started, an additional line
appears at the top of the screen (Touch to return to call). This option
will simulate that line.
Simulate Hardware Keyboard: Simulates a connected keyboard (just use your Mac’s keyboard).
TV Out: Displays a window that will show the contents of the iPhone’s TV Out signal. We will not be using this feature in this book.
Test a few of these out on the HelloSimulator application. Figure 5 shows the application’s reaction to a simulated memory warning.
Figure 5. The iPhone Simulator can test for application handling in several unique conditions.