Using the iPhone Simulator

9/14/2010 4:55:29 PM
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.

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.

Watch Out!

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 magnifier appears.

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.

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.

Generating Multitouch Events

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.

Version: 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 the iOS.

Shake Gesture: Simulate a quick shake of the iPhone.

Lock: 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.

Top 10
Windows Phone 7 Development : Building a Trial Application (part 3) - Verifying Trial and Full Mode & Adding Finishing Touches
User Account Control Is More Than You Think
Sharepoint 2007: Create a New Document
Using Brushesin XAML
iPhone Application Development : Using Switches, Segmented Controls, and Web Views (part 2)
Monitoring a SharePoint 2010 Environment : Understanding Timer Jobs for SharePoint 2010
Separating BPM and SOA Processes : Example-Process for Handling Credit Card Disputes
Exchange Server 2007 : Configure the Client Access Server - Manage OWA
Enumerate Directories and Files
Most View
iPhone Application Development : Implementing a Custom Picker View (part 4) - Tweaking the Picker UI
SharePoint 2010 : Understanding Windows PowerShell Concepts (part 2)
Microsoft XNA Game Studio 3.0 : Writing Your First Program (part 1)
How to beat 2012’s web threats (Part 4)
SQL Azure : Tuning Techniques (part 5) - Provider Statistics & Application Design
Windows Azure : Messaging with the queue - Patterns for message processing
Windows Phone 7 Development : Debugging Application Exceptions (part 2) - Debugging a Web Service Exception
jQuery 1.3 : Modifying table appearance (part 1) - Row highlighting
Recovering from a Disaster in an Exchange Server 2010 Environment : Recovering from a Complete Server Failure
Sharepoint 2010 : BCS Architecture - Presentation & Core Components
Securing SharePoint Sites with Forefront TMG 2010 (part 2) - Creating a SharePoint Publishing Rule Using Forefront TMG
Visual Studio 2010 : Writing Custom Facebook Applications - Querying Data from Facebook
Sharepoint 2007: Edit the Properties of a File or a List Item
Accessing Silverlight Content with JavaScript
iPhone 3D Programming : Adding Depth and Realism - Lighting Up (part 1)
Exchange Server 2010 : Utilize the Availability Options for Servers Based on Role (part 2) - Increase Mailbox Database Availability
Expert computing advice (Part 3) - Using heritage format files & Free Screen Capture Software
Parallel Programming : Task Relationships (part 2) - Parent and Child Tasks
Microsoft XNA Game Studio 3.0 : Getting Player Input - Using the Keyboard