常用的单例类有:
cocoa Touch框架:UIApplication;NSUserDefaults;NSNotification;NSBundle;
cocoa框架:NSFileManager;NSWorkspace;NSApplication.
The UIApplication class provides a centralized point of control and coordination for apps running on iOS. Every app must have exactly one instance of UIApplication (or a subclass of UIApplication). When an app is launched, the UIApplicationMain function is called;当应用程序被启动后,就会唤醒UIApplicationMain的功能;UIApplicationMain among its other tasks, this function creates a singleton UIApplication object. Thereafter you access this object by invoking唤起 the sharedApplication class method.
A major role of a UIApplication object is to handle the initial routing of incoming user events. It also dispatches调度 action messages forwarded to it by control objects (UIControl) to the appropriate target objects. In addition, the UIApplication object maintains a list of all the windows (UIWindow objects) currently open in the app, so through those it can retrieve any of the app’s UIView objects. The app object is typically assigned a delegate, an object that the app informs of significant runtime events—for example, app launch, low-memory warnings, and app termination—giving it an opportunity to respond appropriately.
Apps can cooperatively handle a resource such as an email or an image file through the openURL: method. For example, an app opening an email URL with this method may cause the mail client to launch and display the message.
The programmatic interfaces of UIApplication allow you to manage behavior that is specific to the device. Use this object to do the following:
Control the app’s response to changes in interface orientation.
Temporarily suspend incoming touch events.
Turn proximity sensing (of the user’s face) off and on again.
Register for remote notifications.
Trigger the undo-redo UI (applicationSupportsShakeToEdit).
Determine whether an installed app can open a URL (canOpenURL:).
Extend the execution of the app so that it can finish a task in the background.
Schedule and cancel local notifications.
Coordinate the reception of remote-control events.
Perform app-level state restoration tasks.
UIApplication defines a delegate that must adopt抛弃 the UIApplicationDelegate protocol and implement部署 some of the protocol methods.
Subclassing Notes
Most apps should never need to subclass the UIApplication class. Most apps use an app delegate to manage interactions between the system and the app.
The only situation where you might need to subclass UIApplication is when you are implementing a custom event or action dispatching mechanism. In that situation, you might override the sendEvent: or sendAction:to:from:forEvent: methods to implement that mechanism. However, the cases where this is required are very rare and should be avoided whenever possible.
+ sharedApplication
Returns the singleton app instance.
Declaration声明
SWIFT
class func sharedApplication() -> UIApplication
OBJECTIVE-C
+ (UIApplication *)sharedApplication
Return Value返回值
The app instance is created in the UIApplicationMain function.
Import Statement
OBJECTIVE-C
@import UIKit;
SWIFT
import UIKit