Execute code in Launch Screen

Xcode allows to create launch screen in .xib files via Interface Builder. Is it possible to execute some code with the xib, just like in usual view controllers? It would be great if we can set different text/images/etc while app launching.


Solution 1:

No, it's not possible.

When launch screen is being displayed your app will be in loading state.

Even the - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions will not be completely executed while the launch screen is displayed.

So it's clear that, you don't have any access to your app and so at this point you can't execute any code.

Solution 2:

I was trying to do the same thing here. :)

I really liked some of the apps, in which they do a little dynamic greeting text and image each time the app is launched, such as "You look good today!", "Today is Friday, a wonderful day", etc, which is very cute.

I did some search, below is how to do it: (My code is XCode 7, with a launchscreen.xib file)

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
    var customizedLaunchScreenView: UIView?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

        // Override point for customization after application launch.
        application.statusBarHidden = true

        // customized launch screen
        if let window = self.window {
            self.customizedLaunchScreenView = UIView(frame: window.bounds)
            self.customizedLaunchScreenView?.backgroundColor = UIColor.greenColor()

            self.window?.makeKeyAndVisible()
            self.window?.addSubview(self.customizedLaunchScreenView!)
            self.window?.bringSubviewToFront(self.customizedLaunchScreenView!)
            UIView.animateWithDuration(1, delay: 2, options: .CurveEaseOut,
                                       animations: { () -> Void in
                                        self.customizedLaunchScreenView?.alpha = 0 },
                                       completion: { _ in
                                        self.customizedLaunchScreenView?.removeFromSuperview() })
        }

        return true
    }

// other stuff ...

}

Just do what ever you wanted to show, text, images, animations, etc. inside the customizedLaunchScreenView here.

At the end of the launching, just fade out this customized UIView using alpha value change, then remove it completely.

How cool is that? I absolutely love it!

Hope it helps.