Setting Up a New Xcode Project
The following are instructions on how to set up a new Xcode project. Please make sure you followed these instructions carefully to ensure your project runs successfully.
Last updated
The following are instructions on how to set up a new Xcode project. Please make sure you followed these instructions carefully to ensure your project runs successfully.
Last updated
Let's begin by creating our first, new Xcode project! To do so, launch the Xcode application and press the Create a new Xcode project option
Now you will now be prompted to choose the type of application you want to create. Apple provides a number of application templates for you to use. For the duration of this class, however, we will always be creating a Single View App, which will create a single non-complex view app for you to add to. If you have a newer Xcode version, you should click the option that says App and make sure you select iOS for your category–not multiplatform.
Next, give a name for your new application. Please also make sure that you've selected Language: Swift and User Interface: Storyboard. After doing so, you may click Next, and then save your application in the directory that you want to save it in. You can keep the default settings for the other fields.
Now that you’ve created a new Xcode project, you can start working! Before you start, however, we are going to go over some small tips and tricks for using and navigating to change the type of device to build your application on, click on the button next to the Play Button that says code. Firstly, on the left side of the project, you should see the file hierarchy for the project. This allows you to see how the files that your project contains as well as navigate or change any of them by simply clicking on the file that you want to change.
Now, if you look to the top-left of the project, you should see a play button next to another button that should say something like SampleApplication > iPhone 12 Pro Max. The play button is the button that you will want to click when you have written some code and want to see how your application looks. Clicking on the play button (or clicking CMD + R) will run your application on a simulator device.
iPhone 12 Pro Max (it may something different on your application, i.e. iPhone X). This should then bring down a dropdown menu of all the possible devices that you can run your application on. Simply click on the device that you want and then you are done!
Now, let's say that you want to create a new Swift file. To do so, navigate to File -> New -> File... or simply press CMD + N. You should now see a window asking you what type of file you want to create. You will often be choosing between either an empty Swift File or a Cocoa Touch Class, depending on whether you want some of the boilerplate code for built-in swift classes (you will learn more about this as you go through the course).
For now, we will continue with a Cocoa Touch Class. After clicking next, you should see a window like the one below. Next to the Class: textfield, put in the name of your new file. Then, in the Subclass of: dropdown, find and select the type of file that you want to create. In the screenshot below, I want to create a new UIViewController file. Lastly, click Next and then you are done!
There are multiple ways to layout views to a screen in iOS development – some examples include frame-based, storyboards, and programmatic AutoLayout. In this course, we'll be learning programmatic AutoLayout.
To use Swift programmatically, there are a few things you need to do:
Delete Main.storyboard
. Newer Xcode versions might be just Main
. Right click on the file, select Delete --> Move to Trash.
2. Navigate to Info.plist
and select the minus symbol next to Main storyboard file base name
to delete it.
If you have a newer version of Xcode, you might not see this entry in Info.plist
. Instead, you can either:
Search for main
like so and click on the first result
Then delete Main
for UIKit Main Storyboard File Base Name
2. Click on the app name in the left pane as shown in the image, select the app name under Targets
and under the Deployment Info
section within the General
tab, change Main Interface
to LaunchScreen
The following steps may be different for you depending on which version of Xcode you are running. If you are running the newer version of Xcode, you will see SceneDelegate.swift
in your sidebar. If not, you will only see AppDelegate.swift
.
3. If you have SceneDelegate.swift
in the sidebar, expand the Application Scene Manifest
option all the way. Select the minus symbol next to Storyboard Name
to delete it.
4. SceneDelegate OR AppDelegate
SceneDelegate.swift
: In SceneDelegate.swift
, go to the first function, scene(_:willConnectTo:options:)
. This function is called whenever a scene is added to your app. In our case of setting up the project, we're dealing with the first scene of the app -- i.e. the first screen of you app. Write the following lines of code:
Line-by-line, here's what this code is doing:
21. Create a UIWindow
instance that will act as our key window, i.e. the main "container" for our app . Apple documentation refers to this as a "backdrop for your app's user interface".
22. Create an instance of ViewController
, which is the view that we want to be the base of our application, i.e. the view that shows when a user first opens the app.
23. Set the window's rootViewController
to be a UINavigationController
with our rootVC as its root view.
- If you don't want to embed your app in a navigation controller (discussed in Lecture 3),
you would just do window.rootViewController = rootVC
on this line.
24. Set our newly created window
to the SceneDelegate
's window
property.
25. Make our window visible! (Instead of hidden)
SceneDelegate.swift
: In AppDelegate.swift
, add this instance variable to the top of the class:
Now go into the first function, application(_:didFinishLaunchingWithOptions:)
. This function is called whenever your app launches. This is where we want to set up our app. Write the following lines of code:
Line-by-line, here's what this code is doing:
14. Declare a UIWindow
object at the top level, to be instantiated elsewhere in the file.
18. Create an instance of the window
object with the size of the phone's screen, which will serve as the main container for our app.
19. Create an instance of ViewController
, which is the view that we want to be the base of our application, i.e. the view that shows when a user first opens the app.
20. Set the window's rootViewController
to be our viewController from line 19.
21. Set our newly created window
to the AppDelegate
's window
property.
22. Make our window visible.