At WWDC 2014, Apple announced a new direction with its innovative software programming language, named Swift, for future iOS and MacOS app development. The adoption of Swift has been phenomenal over the last 5+ years: open source language development, availability of an iPad Swift playground, language consistency, SwiftUI, and numerous features in Xcode, including SwiftUI preview, to support app development. This post details the steps to create a Swift 5 code-only (i.e., no storyboards) project within Xcode 11.

Getting Started: Creating the Code-only App

  • Create a new Xcode project using the ‘Single View Application‘ as the iOS template. Select ‘Swift‘ as the language. The other options include ‘Use Core Data‘, ‘Include Unit Tests‘, and ‘Include UI Tests‘.
  • There are two options for ‘User Interface‘ in Xcode 11: ‘SwiftUI‘and ‘Storyboard‘. If ‘SwiftUI‘ is selected, a main storyboard doesn’t exist, a ‘ContentView.swift‘ file is created instead to support SwiftUI code development, and the rest of this post doesn’t apply.
  • If ‘Storyboard‘ is selected, a ‘Main.storyboard‘ file and a ‘ViewController.swift‘ file are created. The rest of this post assumes that ‘Storyboard‘ was selected as the ‘User Interface‘.

Removal of Main.storyboard

  • Delete the file ‘Main.storyboard‘ from the ‘Project Navigator‘ pane.
  • Select the project in the ‘Project Navigator‘ pane, then select the target in the middle editor pane, and finally, click on the ‘General‘ tab). Clear the ‘Main Interface‘ field.
  • Select the ‘Info.plist‘ file in the ‘Project Navigator‘ pane. Expand the ‘Application Scene Manifest‘ line in the editor pane as shown and remove the line titled ‘Storyboard Name‘. At this point, all evidence of a main storyboard are gone.

Code Changes

Upon creation of the Swift 5 project within Xcode 11, a new file ‘SceneDelegate.swift‘ in iOS 13 was created along with the expected ‘ViewController.swift‘ file. Both of these files require some edits to support a code-only Swift 5 iOS 13 app in Xcode 11.

  • In the ‘SceneDelegate.swift‘ file, I replaced the existing code shown first with the updated code shown afterwards to provide the start up functionality.
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        
        guard let _ = (scene as? UIWindowScene) else { return }

    }
class SceneDelegate: UIResponder, UIWindowSceneDelegate {

    var window: UIWindow?

    func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
        // Use this method to optionally configure and attach the UIWindow `window` to the provided UIWindowScene `scene`.
        // If using a storyboard, the `window` property will automatically be initialized and attached to the scene.
        // This delegate does not imply the connecting scene or session are new (see `application:configurationForConnectingSceneSession` instead).
        
        guard let windowScene = (scene as? UIWindowScene) else { return }
        window = UIWindow(frame: windowScene.coordinateSpace.bounds)
        window?.windowScene = windowScene
        window?.rootViewController = ViewController()
        window?.makeKeyAndVisible()
    }
  • In the ‘ViewController.swift‘ file, I added a simple background color to the existing code to provide the initial view functionality.
import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = .blue
    }
}

Compile and Run

At this point, the project should not show any errors or warnings and should compile. Select a device or simulated device, as appropriate, and compile/run. The app should launch and, in this case, display a blue background. This makes an easy way to establish a starting point to develop a code-only app.

Reference Information:

Xcode 11 – Create a New Project – No Storyboard video by Sean Allen.

Credits

The Xcode and Swift logos are copyrights and trademarks of Apple.