Hands-On Full:Stack Development with Swift
上QQ阅读APP看书,第一时间看更新

Creating an app

To create an app, you will need to have Xcode installed. You can get it from Apple's App Store. Once you open Xcode, you will be greeted with the Welcome to Xcode modal. This is where you will see your most recent projects:

We will get started by selecting the Create a new Xcode project option from the dialog. If you want to just explore Swift language, you can select Get started with a playground.

Playgrounds are a hybrid between a text editor for Swift code and a code runner where you can see the result of your code as you type, making it easy to learn the language or try out something quickly.

This will open another dialog where we will be prompted to select the template for our project. There are several templates to choose from, but for our app, the Single View App is a good template to begin with:

Give your app a name and make sure the language selected is Swift (we will not check core data or other test options) and click Next:

It will prompt you to select a folder where this project will be created. Select a folder, click Create, and Xcode will generate the project and open it.

The generated project will contain two Swift files, two storyboards, an Assets folder, and Info.plist. Let's explore each of these files in detail:

  • AppDelegate.swift: This is the entry point of our app and where we have some callback functions that get called when the app gets started, goes in the background, or becomes active. This is a good file where we perform any actions we want to do before the app becomes inactive or becomes active again, such as loading and saving the state of the app.
  • ViewController.swift: This is the default and first View Controller that is created by our app. View controllers are in charge of handling the state of the view and reacting on actions by the user, such as a button tap or refreshing the view when the network request is completed.
  • Main.storyboard: This is the main file that contains the visual representation of the app. It shows and defines how the app progresses from one view to another. You can get the full picture of all of the states in the app by viewing the Storyboard in a simple iOS application.
  • LaunchScreen.storyboard: This is the storyboard file that contains the view that is shown when the app starts up.
  • Assets.xcassets: This is a folder where you place all of your image assets, including app icon images.
  • Info.plist: This is the config file in XML format that contains the configuration for your app and also contains settings to request permission for certain capabilities of the phone, such as user location: