Get your app launch time with Swift

In this post, we’ll be looking at how to get your app launch time with Swift. App launch times are fundamental to a good user experience and as an iOS Developer, you should be looking to optimise this as much as you can.

In iOS, the application launch cycle does a lot of things before it even calls “didFinishLaunchingWithOptions” inside your AppDelegate. It’s important to understand what iOS actually does before it gives you control so you can properly measure the launch time. I won’t go into the specifics of this in this post but if you’re interested, Ole Begemann has written a great post on the iOS app launch sequence that I highly recommend you read. In it he references Objective-C, but the fundamentals are the same and can be applied to Swift very easily.

For us, we want to know how quickly our app is launching. The first thing we need to do is create a new Swift file called main.swift. It should look like this:

By creating a main.swift file we’re telling Xcode where our application kicks things off. This is by default not required, but adding one ourselves means we need to then call UIApplicationMain() and tell it which class is our AppDelegate. We do this via NSStringFromClass(AppDelegate).

And now, in our AppDelegate.swift file, make the following changes:

  1. Comment the line @UIApplicationMain. Among other things, this line essentially tells the compiler this class is our AppDelegate. Since we are doing that manually in our main.swift file, we don’t need or want this.
  2. Here we’re adding an operation onto the main queue that will log our launch time to the console. The reason we’re using GCD here is so we make sure “didFinishLaunchingWithOptions” has actually finished before we execute this piece of code.

And that’s it! If you make these changes you’ll see your application launch time printed to the console. The lower you can get it, the better 🙂

Leave a comment