Search code examples
iosswiftappdelegatexcode-storyboard

Swift Code in App Delegate to Load Storyboard Causes Crash


I have an universal app I am writing and I initially wrote it using two storyboard files. I had code in the App Delegate didFinishLaunchingWithOptions routine to decide which storyboard to load and away it would go. I have since realised that that was a silly thing to do because I had duplicate code, so I have deleted one storyboard and made the other universal. I have fixed up the VCs to point at the right class and everything. However, my app now refuses to launch. When I run it in the sim, it gives me the error

The app delegate must implement the window property if it wants to use a main storyboard file.

.

Here is my code in App Delegate:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    // Override point for customization after application launch.

    var deviceIdiom = UIDevice.currentDevice().userInterfaceIdiom
    if deviceIdiom == UIUserInterfaceIdiom.Phone {
        strDevice = "iPhone"
    } else if deviceIdiom == UIUserInterfaceIdiom.Pad {
        strDevice = "iPad"
    } else {
        strDevice = "Unknown"
    }

    return true
}

What am I doing wrong?

(Questions I've already looked at but didn't help): Unique Issue displaying first storyboard scene in Xcode
The app delegate must implement the window property if it wants to use a main storyboard file
Managing two storyboards in App Delegate
How to manually set which storyboard view to show in app delegate
Swift - Load storyboard programatically


Solution

  • Your app delegate needs to start like such:

    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate {
    
      var window: UIWindow?
    
      //...
    }