Freelance iOS Developer, Rapidly Aging Punk

Replace @NSApplicationMain or @UIApplicationMain with main.swift

May 16, 2015 at 02:00PM

Let's say you want to run some code before your AppDelegate launches in a Swift application (like receipt validation.)

First, get rid of the @NSApplicationMain or @UIApplicationMain from your AppDelegate.swift file:

import UIKit

//@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

Next, File->New->File... to create a Swift file and name it main.swift. Here's how it should look for an iOS app:

import UIKit

//Pre-launch code here.

UIApplicationMain(Process.argc, Process.unsafeArgv, NSStringFromClass(UIApplication), NSStringFromClass(AppDelegate))

And Mac:

import Cocoa

//Pre-launch code here.

NSApplicationMain(Process.argc, Process.unsafeArgv)