The first step in creating a Mac app is to create a project in Xcode. This article walks you through creating a Mac app project and explains the files Xcode creates for the project.
Creating the Project
Choose File > New > Project to create a new project. The New Project Assistant window opens.
Select macOS from the top of the window to show the Mac project templates. Select Cocoa App from the list of application project templates. Click the Next button.
Now things become a little more complicated. You must supply the following information for the new project so Apple can set the project the way you want:
- Product name
- Organization name
- Organization identifier
- Whether to use storyboards or xib files for the user interface
- Whether or not to create a document-based app
- Whether or not to use Core Data
- Whether or not to add testing targets
The product name is the name of the project. It’s also the name of the application. The menu bar for the app will have a menu with the product name that has menu items to quit and hide the app that use the product name.
It is possible to rename a project after creating it. But choosing a good name for the project when you create it makes things easier.
The Team menu is for people who have a paid Apple developer account. Choose your team from the menu, and Xcode will use it to code sign your app. If you don’t have a paid developer account, choose None from the Team menu.
The organization name appears in the copyright notice of the source code files you create for your project. If you don’t have an organization name, use your name.
The organization identifier uniquely identifies the organization. The identifier takes the form
com.OrganizationName. There should be no spaces in the organization identifier.
The Language menu determines the programming language for the source code files Xcode creates for the project. There are two language choices for Mac apps: Swift and Objective-C.
Storyboards or Xib Files
The Use Storyboards checkbox determines whether Xcode creates a storyboard or a xib file for your project’s user interface. Storyboards let you see the app’s entire user interface in one place. Storyboards also make working with view controllers easier. But xib files make creating master-detail interfaces easier.
Xcode 11 Update
Xcode 11 replaces the Use Storyboards checkbox with a User Interface menu item. For a Mac app the user interface options are SwiftUI, Storyboards, and Xib Files.
The Create Document-Based Application checkbox determines whether your app uses Apple’s document architecture. Document-based apps let people create, open, and save documents as separate files that they can share with other people. Examples of document-based apps include text editors, spreadsheets, and drawing apps.
If you decide to create a document-based app, use the Document Extension text field to supply the file extension for the document files your app creates.
The Use Core Data checkbox determines whether your app uses Core Data for your app’s data. Core Data is a complex technology so I recommend not using it when starting out making Mac apps.
The Include Unit Tests and Include UI tests checkboxes determine whether Xcode creates targets for unit testing your app’s code and testing your app’s user interface. If you’re learning Mac development, you can deselect the checkboxes. When you start making more complex apps, unit testing and UI testing can help you write better code and design better user interfaces.
Finishing Creating the Project
When you supply all the information Xcode demands for the project and click the Next button, the last step is to choose a location to save your project. At the bottom of the Save panel you should see the following controls:
If you select the Create Git repository on my Mac checkbox, Xcode will place your project under version control. You can get away with not using version control for small projects. If you write an app you want to share with the public, I recommend using version control so you can track changes to files and go back to earlier versions of files if you make mistakes. I wrote a book on version control for Mac developers if you want to learn more about version control.
The project navigator on the left side of the project window lists the files in your project. The following screenshot shows the files Xcode creates for a Mac project that uses Swift, uses storyboards, is not document-based, and does not use Core Data:
The files in the project name folder (ButtonClickCounter in the screenshot) are the most interesting.
When you’re starting Mac development, focus on the source code and storyboard files. You’ll use the other files and folders when you get closer to shipping an app.
Select a file from the project navigator to open it in Xcode’s editor.
AppDelegate.swift file lets you add code to run when your app launches and terminates.
ViewController.swift file contains a class file for the view controller Xcode creates in the storyboard. A view controller manages a view. You will create a view controller class for every view controller you add to the storyboard.
Projects that use xib files instead of storyboards do not have a view controller source code file.
The Assets folder contains the images in your project. The most common use of the Assets folder is to add an icon for your app.
The storyboard contains your app’s user interface. Select the storyboard to open it and build your app’s user interface.
Info.plist file contains information about your app, such as a copyright notice.
While you can edit the
Info.plist file directly, it’s usually easier to use Xcode’s project editor. Select the project (it’s the first item) from the project navigator to open the project editor. Select the app target from the left side of the project editor. Click the Info button at the top of the project editor to start editing
The entitlements file contains a list of Apple technology capabilities that your app supports. If you select the entitlements file, you will notice that the App Sandbox is turned on.
While you can add and remove capabilities from the entitlements file, it’s usually easier to do it from Xcode’s project editor. Select the app target from the left side of the project editor. Click the Capabilities button at the top of the project editor.
If you want your app to support things like iCloud and push notifications, turn the switch from Off to On. Keep in mind that most of the capabilities require your app to be code signed, which means you have to be a paid member of Apple’s developer program.
Exercise 1: Run the Project
Choose Product > Run in Xcode or click the Run button in the upper left corner of the project window to run the project. An empty window should appear.
Exercise 2: Experiment with Project Options
Create different types of projects and compare the files that are created. Create a project that doesn’t use storyboards. Create a document-based app project. Create a project that uses Core Data.