- Creating New Projects
- Building Hello World the Template Way
- Using the Simulator
- The Minimalist Hello World
- Converting Interface Builder Files to Their Objective-C Equivalents
- Using the Debugger
- Memory Management
- Recipe: Using Instruments to Detect Leaks
- Recipe: Using Instruments to Monitor Cached Object Allocations
- Analyzing Your Code
- Building for the iOS Device
- Detecting Simulator Builds with Compile-Time Checks
- Performing Runtime Compatibility Checks
- Pragma Marks
- Preparing for Distribution
- Over-the-Air Ad Hoc Distribution
- Submitting to the App Store
- Summary
Using the Simulator
The iOS SDK simulator makes it possible to test applications on the Macintosh using many of the same actions a user would perform on an actual device. Because the Macintosh is not a handheld touch-based mobile system, you must use menus, keyboard shortcuts, and the mouse or trackpad to approximate iPhone-style interactions. Table 3-1 shows how to perform these tasks via the simulator.
Table 3-1. Simulator Equivalents for iPhone Actions
Action |
Simulator Equivalent |
Selecting the device |
Use Hardware > Device to simulate an original iPhone, Retina iPhone, or iPad-style device. Firmware versions are selectable via Hardware > Version. |
Rotating the device |
Hardware > Rotate Left (Command-left arrow) and Hardware > Rotate Right (Command-right arrow). The simulator supports all four major interface orientations: portrait, landscape left, landscape right, and portrait upside down. You cannot simulate face-up or face-down orientations. |
Shaking the device |
Hardware > Shake Gesture (Command-Control-Z). This simulates a shake using a motion event but does not simulate other accelerometer actions. I encourage you to avoid building applications that depend on users shaking devices, no matter how cool the feature appears. |
Pressing the Home key |
Click the Home button on the simulator screen or choose Hardware > Home (Command-Shift-H). |
Locking the device |
Hardware > Lock (Command-L). |
Tapping and double-tapping |
Click with the mouse, either a single- or double-click. |
Tapping on the keyboard |
Click the virtual keyboard or type on the Mac keyboard. You can use many Mac-style shortcuts for ease of testing, including Command-A, Command-C, and so on. |
Dragging, swiping, and flicking |
Click, drag, and release with the mouse. The speed of the drag determines the action. For flicks, drag very quickly. |
Pinching in or out |
Press and hold the Option key on your keyboard. When the two dots appear, drag them toward each other or away from each other. Hold down the Shift key to move the dot's origin point. |
Running out of memory |
Hardware > Simulate Memory Warning. This allows you to simulate a condition of low available memory, letting you test how your application responds. |
In-progress phone call (visual display only) |
Hardware > Toggle In-Call Status Bar. On the iPhone, you can run an application while on a phone call. The in-call bar appears at the top of the screen for the duration of the call. |
Attaching a keyboard |
Simulate the detection of a Bluetooth or docked hardware keyboard by selecting Hardware > Simulate Hardware Keyboard. |
Attaching TV Out cables |
Choose Hardware > TV Out to simulate the attachment of a VGA or HDMI cable to the dock connector. Use this to test your external screen code, and specifically to catch screenattached and -detached notifications. A floating window shows the simulated output. |
Changing zoom |
Change the magnification of the simulator by selecting Window > Scale. Choose from 100%, 75%, and 50%. |
Simulating a printer |
Choose File > Open Printer Simulator to test your software with AirPrint. You can also use this simulator to test printing from a device. The printed output opens in Preview. |
Capturing screenshots |
Choose File > Save Screen Shot (Command-S) or copy the screen with Edit > Copy Screen (Command-Control-C). |
Setting a simulated location |
Use the Debug > Location menu to simulate where the iPhone is being used. Choose from a (stationary) custom location, Apple's HQ, Apple Stores, a city bike ride/run, or a drive down the freeway. |
Slowing down animations |
Choose Debug > Toggle Slow Animations to allow you to better view animations over a longer period of time. Use this feature to spot inconsistencies and flaws in your animations. |
Highlighting potentialing trouble spots |
Use the four Debug > Color options to locate potential presentation issues. The items, which are toggled on and off via menu selection, include blended layers, copied images, misaligned images, and elements rendered off-screen. |
Resetting the simulator |
Choose iOS Simulator > Reset Contents and Settings to restore your simulator to its "factory fresh" original condition, deleting all current applications, settings, and user data. |
Simulator: Behind the Scenes
Because the simulator runs on a Macintosh, Xcode compiles simulated applications for the Intel chip. Your application basically runs natively on the Macintosh within the simulator using a set of Intel-based frameworks that mirror the frameworks installed with iOS onto actual units. The simulator versions of these frameworks are typically located in the Xcode developer directory, in /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library or some similar location. The actual location will vary by the version of the SDK you are using and where you have installed the SDK. The /Developer folder is the default location, but you can easily override this.
You can find your applications in your home’s Library/Application Support folder. They are stored in iPhone Simulator/ in one of many firmware-specific folders, such as 3.1.2/, 4.2/, and 6.1/ under User/Applications/. It’s helpful to visit these folders to peek under the hood and see how applications get deployed to the iPhone; these User/Applications/ folders mimic device installations. Other interesting development folders include the following:
- /Developer/Platforms/iPhoneSimulator.platform/Developer/Applications—Default location of the actual iPhone simulator application.
- /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulatorX.X.sdk/Applications—Location of the simulator’s built-in applications, including Mobile Safari, the Address Book, and so forth. Replace X.X with the firmware version.
- /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.0.sdk/System/Library/Frameworks—Location of the Cocoa Touch frameworks you can link to from your application.
- /Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/Application—Location of the individual project templates shown in Figure 3-1.
- ~/Library/MobileDevice/Provisioning Profiles—Folder that stores your iPhone Portal provisioning profiles.
- ~/Library/Developer/Xcode/Archives—Folder that stores archives built with the Build > Build and Archive option in Xcode. Archived applications appear in your Xcode Organizer, where they can be validated, shared, and submitted to iTunes Connect.
- ~/Library/Developer/Xcode/DerivedData—Build and log folder.
- ~/Library/Developer/Xcode/Snapshots—Project version control snapshots.
- ~/Library/Developer/Shared/Xcode/Screenshots/—Folder for screenshots taken with the Organizer.
- ~/Library/Developer/Shared/Project Templates/—Add custom templates to this folder to have them appear in the New Project screen.
- ~/Library/MobileDevice/Software Images—Folder that stores iOS firmware files (.ipsw files) that can be installed onto your devices.
- ~/Library/Application Support/MobileSync/Backup—Folder where iTunes stores iPhone, iPod touch, and iPad backup files.
Each application is stored in an individual sandbox. The name of the sandbox is random, using a unique code (generated by CFUUIDCreateString()). You can zip up a sandbox folder and be able to share it between Macintoshes. The other Macintosh will need Xcode to be installed in able to access the simulator and its frameworks.
Each sandbox name hides the application it’s hosting, so you must peek inside to see what’s there. Inside you find the application bundle (HelloWorld.app, for example), a Documents folder, a Library folder, and a temporary (/tmp) folder. While running, each application is limited to accessing these local folders. They cannot use the main user library as applications might on a Macintosh.
With the exception of the Library/Caches folder, all the materials in an application’s Documents and Library folders are backed up by iTunes when deployed to a device. The tmp folder is not backed up by iTunes. Use the Caches folder to store large, changing application-support data files that need to persist. Use the tmp folder for materials that iOS can dispose of between application launches.
If you want to clean out your applications’ sandbox folders, you can delete files directly while the simulator is not running. You can also delete all the simulator data by choosing iPhone Simulator > Reset Contents and Settings from the simulator itself. This erases applications, their sandboxes, and any current settings, such as nondefault language choices, that affect how your simulator runs.
Alternatively, use the press-and-hold-until-it-jiggles interface on the simulator that you’re used to on the iPhone device itself. After you press and hold any icon for a few seconds, the application icons start to jiggle. Once in this edit mode, you can move icons around or press the corner X icon to delete applications along with their data. Press the Home button to exit edit mode.
Although applications cannot access the user library folder, you can. If you want to edit the simulator’s library, the files are stored in the iPhone Simulator/User/Library folder in your home Application Support folder. Editing your library lets you test applications that depend on the address book, for example. You can load different address book sqlitedb files into Library/AddressBook to test your source with just a few or many contacts.
Sharing Simulator Applications
Simulator-compiled applications provide an important way to share test builds when developers are denied access to new hardware or when beta firmware is not widely distributed. They can also support interface design testing in advance of actual device deployment. Although unsuitable for full debugging and end-user usability tests (see Chapter 1, “Introducing the iOS SDK”), simulator builds do have a role and a purpose in iOS application life cycle.
To share an app, zip up its entire sandbox folder from one Macintosh and then extract it to another Mac’s simulator application folder.