How to create an Empty Application in Xcode without Storyboard

76,108

Solution 1

There is no option in XCode6 and above versions for directly creating an Empty Application as in XCode5 and earlier. But still we can create an application without Storyboard by following these steps:

  1. Create a Single View Application.
  2. Remove Main.storyboard and LaunchScreen.xib (select them, right-click, and choose to either remove them from the project, or delete them completely).
  3. Remove "Main storyboard file base name" and "Launch screen interface file base name" entries in Info.plist file.
  4. Open AppDelegate.m, and edit applicationDidFinishLaunchingWithOptions so that it looks like this:

Swift 3 and above:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool 
    {
        self.window = UIWindow(frame: UIScreen.main.bounds)
        self.window?.backgroundColor = UIColor.white
        self.window?.makeKeyAndVisible()
        return true
    }

Swift 2.x:

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool 
    {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
        self.window?.backgroundColor = UIColor.whiteColor()
        self.window?.makeKeyAndVisible()
        return true
    }

Objective-C:

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
        self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        // Override point for customization after application launch.
        self.window.rootViewController = [[ViewController alloc] init];
        self.window.backgroundColor = [UIColor whiteColor];
        [self.window makeKeyAndVisible];
        return YES;
    }

Solution 2

A simple approach would be to copy the XCode 5's Empty Application template to XCode's templates directory.

You can download XCode 5's Empty Application template from here, then unzip it and copy to /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/Xcode/Templates/Project Templates/iOS/Application directory.

P.S this approach works with swift as well!

Edit
As suggested by @harrisg in a comment below, You can place the above mentioned template in ~/Library/Developer/Xcode/Templates/Project Templates/iOS/Application/ folder so that it may be available even if Xcode gets updated.
And if there is no such directory present, then you might have to create this directory structure: Templates/Project Templates/iOS/Application/ in ~/Library/Developer/Xcode/

Using this simple approach i'm able to create an Empty Application in XCode 6. (Screenshot attached below)

Xcode Empty Application Template Hope this helps!

Solution 3

There are a few more steps that you need to do:

  1. To add a prefix file. (If you need it)
  2. To add a default launch image, otherwise the app size will be 320x480 on iPhone 5.

So here is a full tutorial:

  1. remove Main.storyboard file
  2. remove LaunchScreen.xib file
  3. remove "Main storyboard file base name" property in Info.plist
  4. remove "Launch screen interface file base name" property in Info.plist
  5. add "[app name]-Prefix.pch" file to supporting files with contents:

    #import <Availability.h>
    
    #ifndef __IPHONE_3_0
    #warning "This project uses features only available in iOS SDK 3.0 and later."
    #endif
    
    #ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #endif
    
  6. add "$SRCROOT/$PROJECT_NAME/[pch file name]" to project settings -> Build Settings -> Apple LLVM 6.0 - Language -> "Prefix Header"

  7. add "YES" to to project settings -> Build Settings -> Apple LLVM 6.0 - Language -> "Precompile Prefix Header"
  8. open "Image.xcassets" and add LaunchImage
  9. build the project, then there will be a warning about missing default launch image, just press on the warning and select to add the default, this will add "Default-568h@2x" OR - If you want to use splash images from "Images.xcassets", go to the project settings -> TARGETS -> General -> in "Launch Images Source" choose to use asset catalog, it will create a new one, you can choose then, which to use as asset catalog from the existing.
  10. implement application:didFinishLaunchingWithOptions: method:

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    //Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    return YES;
    

Solution 4

Akhils answer is totally correct. For those of us using Swift, it would be like this:

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    self.window = UIWindow(frame: UIScreen.mainScreen().bounds)
    self.window?.backgroundColor = UIColor.whiteColor()
    self.window?.makeKeyAndVisible()
    return true
}

Solution 5

Xcode 9.3.1 and Swift 4

  1. First of all you need delete Main.storyboard in Project navigator menu.
  2. Then delete row Main storyboard file base name in Info.plist.
  3. And don't forgot delete cell Main Interface (just remove Main) in your Project Target - General - Deployment Info.
  4. After that steps go to AppDelegate.swift and in function didFinishLaunchingWithOptions write the next:

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
    
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.makeKeyAndVisible()
        window?.rootViewController = UINavigationController(rootViewController: ViewController())
    
        return true
    }
    

Xcode 11.2.1 and Swift 5

  1. First of all you need delete Main.storyboard in Project navigator menu.
  2. Then delete row Main storyboard file base name in Info.plist.
  3. And don't forgot delete cell Main Interface (just remove Main) in your Project Target - General - Deployment Info.
  4. This step is important and it was not in previous versions of Xcode and Swift. In Info.plist go to: Application Scene ManifestScene ConfigurationApplication Session RoleItem 0 and delete here Storyboard.name row.
  5. After that steps go to SceneDelegate and in function scene write the next:

    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()
    }
    
Share:
76,108

Related videos on Youtube

Akhil K C
Author by

Akhil K C

iOS Developer from Kochi, India

Updated on May 07, 2020

Comments

  • Akhil K C
    Akhil K C about 4 years

    Xcode6 has removed the Empty Application template when creating a new project. How can we create an empty application (without Storyboard) in Xcode6 and above, like in earlier versions?

  • Esqarrouth
    Esqarrouth over 9 years
    there is a bounds problem, UIScreen.mainScreen().bounds is printing out :(0.0,0.0,320.0,480.0). why is that?
  • tboyce12
    tboyce12 over 9 years
    I like to keep LaunchScreen.xib and the corresponding Info.plist entry. Without it, the app will not take up the entire screen size on larger iPhones, which is ugly.
  • Mazyod
    Mazyod over 9 years
    I like to keep the Storyboard, myself. It's convenient for creating various stuff, but I need to do the initial setup without relying on storyboards.
  • elliotrock
    elliotrock over 9 years
    Thanks. As a note you will need to replace this template after Xcode updates.
  • harrisg
    harrisg about 9 years
    If you put it into ~/Library/Developer/Xcode/Templates/Project Templates/iOS/Application/ it won't have to be replaced each time Xcode updates
  • S1LENT WARRIOR
    S1LENT WARRIOR about 9 years
    @harrisg couldn't find this directory in Yosemite. Can you confirm its presence? Thanks
  • GoodSp33d
    GoodSp33d over 8 years
    Maybe a bug in Xcode 7 I was not able to remove Main.storyboard at all from the project information settings screen. I used Xcode 5 empty application template as mentioned by @Silentwarrior.
  • bitsand
    bitsand over 8 years
    This approach works with Xcode 7.1, however the LaunchScreen.storyboard is created that some may not want. I ended up using it as it does not interfere with my manually created views.
  • Admin
    Admin over 8 years
    Unless you manually install a root ViewController, this answer generates a runtime error in XCode7: 'NSInternalInconsistencyException', reason: 'Application windows are expected to have a root view controller at the end of application launch'
  • Rizwan Ahmed
    Rizwan Ahmed over 8 years
    Please add this Code also : self.window.rootViewController= [[ViewController alloc]init]; in your AppDelegate.m under didFinishLaunchingWithOptions method. Or else you will get the following error : 'Application windows are expected to have a root view controller at the end of application launch'
  • nburk
    nburk over 8 years
    when using this approach, AutoLayout seems to be disabled. any idea what I can do to enable it again? stackoverflow.com/questions/36173367/…
  • Balaban Mario
    Balaban Mario about 8 years
    Remember to unzip the file into the directory, copying is not enough.
  • Anil Gupta
    Anil Gupta over 7 years
    I used xcode 8 beta 2 and able to create Xibs using Akhil KC steps. But you are missing Defaults image Default Images for iPhone: [email protected] Default-iPhone6.png Default-iPhone6Plus.png Default.png [email protected] iPad : Default-Portrait~ipad.png (768x1024px) Default-Landscape~ipad.png (1024x768px) Default-Portrait@2x~ipad.png (1536x2048px) Default-Landscape@2x~ipad.png (2048x1536px)
  • Rémy
    Rémy over 7 years
    You can also use ".white" for the color on Swift 3 ;-)
  • Giorgio Tempesta
    Giorgio Tempesta over 7 years
    You also need to add #import "ViewController.h" at the top of AppDelegate.m, after #import "AppDelegate.h"
  • Michael Innes
    Michael Innes almost 7 years
    When running in the simulator, I get: Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Could not load NIB in bundle: 'NSBundle </Users/username/Library/Developer/CoreSimulator/Devices/A44‌​1306A-8CA5-4FCD-98C5‌​-8DFAFBFF1E3E/data/C‌​ontainers/Bundle/App‌​lication/63E53C13-36‌​77-44C2-8BEF-61FEAB1‌​69010/Totally Empty.app> (loaded)' with name 'EmptyViewController''
  • peacetype
    peacetype over 6 years
    Using Xcode 9.1 & Swift 4 if I simply delete the storyboard file without manually removing the corresponding reference from .plist I get an error "Could not find a storyboard named 'Main' in bundle". Seems that I must remove the reference manually.
  • Ziad Hilal
    Ziad Hilal over 4 years
    You also have to delete "Main storyboard file base name" from Info.plist, otherwise you'll get the error "Could not find a storyboard named 'Main'"