The UIApplication Singleton
Every iOS application relies on a centralized singleton object to manage app-wide behaviors. This instance is generated at launch and can be accessed globally to perform system-level operations.
// Accessing the core application instance
UIApplication *coreApp = [UIApplication sharedApplication];
Through this singleton, developers can manipulate visual indicators such as the badge count on the app icon or the network activity spinner in the status bar.
// Configure the red badge number on the icon
coreApp.applicationIconBadgeNumber = 5;
// Toggle the network spinner visibility
coreApp.networkActivityIndicatorVisible = YES;
Status Bar Management
Since iOS 7, the status bar can be controlled either centrally via UIApplication or individually by each UIViewController. By default, view controllers take precedence. To enforce centralized control, a specific key (View controller-based status bar appearance) must be added to the project's configuration plist and set to NO.
// UIViewController-based status bar style
- (UIStatusBarStyle)preferredStatusBarStyle {
return UIStatusBarStyleLightContent;
}
- (BOOL)prefersStatusBarHidden {
return NO;
}
Inter-App Communication
The application instance can hand off tasks to other system applications using URL schemes.
UIApplication *systemApp = [UIApplication sharedApplication];
// Initiate a phone call
[systemApp openURL:[NSURL URLWithString:@"tel://0123456789"]];
// Compose a text message
[systemApp openURL:[NSURL URLWithString:@"sms://0123456789"]];
// Draft an email
[systemApp openURL:[NSURL URLWithString:@"mailto://contact@domain.com"]];
// Load a web page in Safari
[systemApp openURL:[NSURL URLWithString:@"https://www.example.com"]];
Handling System Interruptions via Delegation
Mobile applications are frequently interrupted by incoming calls, messages, or system alerts. The UIApplication singleton delegates the responsibility of handling these disruptions to a designated object. This delegate processes lifecycle transitions, memory warnings, and background/foreground state changes. The default delegate class created in a new Xcode project automatically conforms to the UIApplicationDelegate protocol.
Application Launch Sequence
The entry point for an iOS program resides in main.m. The UIApplicationMain function initializes the core components and starts the main run loop.
int main(int argumentCount, char *argumentVector[]) {
@autoreleasepool {
return UIApplicationMain(
argumentCount,
argumentVector,
@"UIApplication",
@"CustomAppDelegate"
);
}
}
The UIApplicationMain function performs four critical tasks: instantiating the UIApplication class, instantiating the specified delegate class, linking the delegate to the application instance, and initiating the primary event loop.
Lifecycle Transitions
The delegate object receives callbacks during significant state transitions.
- (BOOL)application:(UIApplication *)appInstance didFinishLaunchingWithOptions:(NSDictionary *)launchConfig {
// Initialization logic after launch
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)appInstance {
// Paused or interrupted (e.g., incoming call)
}
- (void)applicationDidBecomeActive:(UIApplication *)appInstance {
// Resumed interaction
}
- (void)applicationDidEnterBackground:(UIApplication *)appInstance {
// Save user data and release resources
}
- (void)applicationWillEnterForeground:(UIApplication *)appInstance {
// Restore saved state
}
- (void)applicationWillTerminate:(UIApplication *)appInstance {
// Final cleanup before exit (not called if suspended)
}
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)appInstance {
// Free up non-essential memory
}
Precompiled Headers
Projects traditionally include a prefix header file (.pch) to implicitly import common frameworks across all source files.
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#endif
This file is also ideal for defining global macros, such as a debug-only logging utility.
#ifdef DEBUG
#define DebugLog(...) NSLog(__VA_ARGS__)
#else
#define DebugLog(...)
#endif
Startup Execution Flow
- Execution begins at the
mainfunction. UIApplicationMainconstructs the application and its delegate.- If no storyboard is configured, the delegate's
didFinishLaunchingWithOptions:method manually instantiates a UIWindow, assigns a root view controller, and makes it visible. - If a main storyboard is defined in the configuration, the system automatically creates the UIWindow, instantiates the initial view controller, and displays the view hierarchy.
UIWindow Hierarchy
UIWindow serves as the root container for all visual elements. An app typically possesses a single main window. Views are rendered on screen only because they are attached to this window hierarchy.
// Attaching a view directly (bypasses view controller lifecycle management)
[mainWindow addSubview:customView];
// Assigning a root controller (preferred, manages lifecycle automatically)
mainWindow.rootViewController = primaryViewController;
// Designate as the primary destination for keyboard and touch events
[mainWindow makeKeyAndVisible];
The application singleton maintains an array of all active windows and a reference to the key window.
// Array of all application windows
NSArray *allWindows = [UIApplication sharedApplication].windows;
// The current key window accepting input
UIWindow *activeWindow = [UIApplication sharedApplication].keyWindow;
// Determine which window a specific view belongs to
UIWindow *viewOwner = customView.window;
Project Configuration (Info.plist)
The Info.plist file dictates runtime configurations and should never be removed. Other plist files within the project must avoid using "info" in their names to prevent conflicts.
- CFBundleDevelopmentRegion: Default localization region.
- CFBundleDisplayName: The name displayed under the app icon on the home screen.
- CFBundleIconFile: The primary app icon asset.
- CFBundleVersion: The build version number, incremented for App Store submissions.
- NSMainStoryboardFile: The primary interface storyboard.
- CFBundleIdentifier: The unique reverse-DNS identifier for the application.