In this video, learn about the app's startup phase.
- [Instructor] A lot of things are going on after the user taps the app's icon. The system creates fundamental objects during the startup process, and calls several delegate methods. This lets us add custom logic to the launch sequence, which allows for a flexible and extensible architecture. In the following demo, we'll take a look at the various customization possibilities that allow us to interfere with the application launch. Let's create an iOS app using the single view app tablet.
I call it AppStartup. I'm gonna create a working projects folder for our samples. Previous versions of Xcode used to generate a main.swift file for iOS projects, which contained the code for the main function. The main entry point for every iOS app is the main function. You won't find that file anymore, as it was replaced by the UIApplicationMain attribute.
It's included in the generated AppDelegate source. You can see it here at line 11. The UIApplicationMain attribute generates an implicit main for the app. This is equivalent to calling the UIApplicationMain function. To demonstrate the application launch sequence, we're gonna replace the UIApplicationMain attribute with a custom implementation. First, I comment out the UIApplication attribute in the AppDelegate.
Next, we need to create the main.swift file. It's a Swift file, and it must be called main. Let's create it. All right, the file name is essential. main.swift is a special file that contains the code needed to launch the application. If we named it differently, the code that's inside the file wouldn't be executed, and our app wouldn't start.
Now we need to implement the UIApplicationMain function. But first we need to import the UIKit framework. Commandline.argc represents the number of command line arguments. The next parameter is the list of arguments. The syntax is a bit strange, but we need that to transform the list of command line arguments to the expected pointer type.
Going into the details of pointer management is beyond the scope of this course. So these are the arguments, and we need to create a pointer using the bind memory method. We need to cast the number of arguments to Int. All right, and the principal class name parameter is the name of the UIApplication class or subclass.
I'm going to provide nil, which means that we'll just use UIApplication. The delegate class name is the name of our AppDelegate class. I have to convert it to an NSString using the NSString from class function. Now behind the scenes, this function instantiates the UIApplication object, and the ApplicationDelegate.
It sets the ApplicationDelegate, and performs further housekeeping. For a storyboard-based application, the UIApplicationMain function loads the main storyboard file. It also creates the app's main window, and sets the storyboard's initial view controller as the window's route view controller. The following sequence diagram summarizes the interactions that happen during application launch. The UIApplicationMain also sets up the application's run loop and the main event loop.
Next, the application object invokes the application will finish launching with options on its delegate. This method is not part of the generated code. We need to implement the application will finish launching with option as method, if we wanna run custom logic before the UI state restoration starts. As per documentation, we should show the app's main window from this method if we rely on UI state restoration. Application should restore application state is invoked if we enable state preservation and restoration for our app.
The application state gets restored if we return through from the delegate method. Then, the Application object invokes the application did finish launching with options on its delegate. Here's where we can perform additional setup before the app becomes fully functional. The app's main window is displayed. And finally, the applicationDidBecomeActive delegate method gets called. These are the very first steps of the startup sequence.
- Unified Modeling Language (UML)
- Use case, class, and sequence diagrams
- App state preservation and restoration
- Fundamental architectural design patterns
- Model-view-controller (MVC) and model-view-presenter (MVP)
- Implementing one-way binding
- App stage changes
- Building responsive apps
- SOLID principles
- Exchanging information between apps
- Optimizing your apps to avoid excessive CPU usage