To create the Mac app in this tutorial, you need Xcode 4.4 or later. Xcode is Apple’s integrated development environment (or IDE) for both OS X and iOS development.
Create and Test a New Project
To get started developing your app, you create a new Xcode project.
Launch Xcode (located in the
If you’ve never created or opened a project in Xcode before, you should see a Welcome to Xcode window similar to this:
If you’ve created or opened a project in Xcode before, you might see a project window instead of the Welcome to Xcode window.
In the Welcome to Xcode window, click “Create a new Xcode project” (or choose File > New > Project).
Xcode opens a new window and displays a dialog in which you choose a template. Xcode includes several built-in app templates that you can use to develop common styles of Mac apps.
In the OS X section to the left of the dialog, select Application.
In the main area of the dialog, select Cocoa Application and click Next.
A new dialog appears that prompts you to name your app and choose additional options for your project.
Choose the following options:
In the Product Name field, type
In the Company Identifier field, type the identifier for your company, or
In the App Store Category pop-up, choose None.
Make sure that the Use Automatic Reference Counting option is selected and that the Create Document-Based Application (appears above Document Extension), Use Core Data, and Include Unit Tests options are unselected.
Another dialog appears that allows you to specify where to save your project.
Specify a location for your project (make sure that the Source Control option is unselected) and then click Create.
Xcode opens your new project in a window (called the workspace window), which should look similar to this:
Take a few moments to familiarize yourself with the workspace window that Xcode opens for you. You’ll use the buttons and areas identified in the window below throughout the rest of this tutorial.
If the utilities area in your workspace window is already open (as it is in the window shown above), you can close it for now because you won’t need it until later in the tutorial. The rightmost View button controls the utilities area. When the utilities area is visible, the button looks like this:
If necessary, click the rightmost View button to close the utilities area.
Even though you haven’t yet written any code, you can build and run your app in Xcode.
Click the Run button in the Xcode toolbar (or choose Product > Run).
If a dialog appears asking whether Xcode should enable developer mode on this Mac, click Disable.
Xcode should build your project and launch the app. When your app starts up, it should have a standard menu bar and display a single window.
Test the app.
You can move and resize the window. However, if you close the window, there is no way to get it back. You should also find that menus display when you click them, and if you choose TrackMix > About TrackMix, an About window is displayed.
Quit the app by choosing TrackMix > Quit TrackMix.
Don’t mistakenly choose the Quit command in Xcode, or you’ll quit Xcode. You can also click the Stop button in Xcode.
Right now, your app is not very interesting: it simply displays a blank window. To understand where the blank window comes from, you need to learn about the objects in your code and how they work together to start the app.
Find Out How an App Launches
Because you based your project on an Xcode template, much of the basic app environment is automatically set up when you run the app. For example, Xcode creates an app object which, among a few other things, establishes the run loop. (A run loop registers input sources and enables the delivery of input events to your app.) Most of this work is done by the
NSApplicationMain function, which is supplied for you by the AppKit framework and is automatically called in your project’s
main.m source file.
Make sure the project navigator is open in the navigator area.
The project navigator displays all the files in your project. If the project navigator is not open, click the leftmost button in the navigator selector bar:
Open the Supporting Files folder in the project navigator by clicking the disclosure triangle next to it.
Xcode opens the source file in the editor area of the window, which should look similar to this:
The call to the
NSApplicationMain function creates an instance of the
NSApplication class and an instance of the
AppDelegate class, which is provided for you by the Cocoa Application template. In this tutorial, the singleton instance of this class is referred to as the app delegate. The main job of the app delegate is to provide a window you can access through its
window property. The window object provides a container for the app’s visible content and helps deliver events to other app objects. The app delegate can also perform some app configuration tasks before the app is displayed. You add your custom behavior and logic to the
AppDelegate class and any other classes you create.
The instance of the
NSApplication class, called the app object, loads the main nib file when the app launches. Nib files are an archive of UI elements and other objects. The main nib file,
MainMenu.xib, usually contains the parts of your user interface, such as the menu bar and window, that are visible the entire time your app is running. When a nib file is loaded, the objects it contains are instantiated.
MainMenu.xibunder the TrackMix group in the project navigator.
The file has the extension
.xibbut by convention it is referred to as a nib file. Xcode displays the file on a canvas in the editor area.
If an outline view appears instead of the standard editor, click the Standard editor in the Editor toolbar.
To display the window, click the window icon in the sidebar.
The sidebar contains several items split into two groups by a dividing line. Above the line are placeholders—objects that are not created as part of the nib file itself, but exist externally. Below the line are objects created as part of the nib file:
A menu object
This object is the app’s main menu displayed in the menu bar.
A window object
This object is the window with a plain gray background that you see when the app launches.
An instance of
AppDelegate(a dark blue cube), set to be the app object’s delegate
When the app object has completed its setup, it sends its delegate an
applicationDidFinishLaunching:message. This message gives the delegate an opportunity to configure the user interface and perform other tasks before the app is displayed.
An instance of
NSFontManager(a dark blue cube)
This object manages the app’s font menu, but you won’t use it in this example.
In this chapter you used Xcode to create a new project based on the Cocoa Application template and you built and ran the default app that the template defines. Then you looked at some of the basic pieces of the project, such as the
main.m source file, and the nib file, and learned what objects are created and loaded from the nib file when the app launches.
In the next chapter, you’ll learn how to lay out and configure the user interface without writing any code.
© 2013 Apple Inc. All Rights Reserved. (Last updated: 2013-04-23)