Single-instance design pattern Step-by 003

Source: Internet
Author: User

The common singleton classes are:

Cocoa Touch frame: uiapplication; Nsuserdefaults; Nsnotification; NSBundle;

Cocoa frame: nsfilemanager; Nsworkspace; NSApplication.

The UIApplication class provides a centralized point of control and coordination for apps running on IOS. Every app must has exactly one instance of UIApplication (or a subclass of UIApplication). When a app is launched, the Uiapplicationmain function is called, and when the application is started, the Uiapplicationmain function is awakened; uiapplicationmain Among its and tasks, this function creates a singleton uiapplication object. Thereafter you access this object by invoking evokes the Sharedapplication class method.

A major role of a UIApplication object is to handle the initial routing of incoming user events. It also dispatches dispatch 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, the app informs of significant runtime Events-for example, App launch, low-memory warnings, and app termination-giving it a opportunity to respond appropriately.

Apps can cooperatively handle a resource such as an e-mail or an image file through the Openurl:method. For example, an app opening a email URL with this method may cause the mail client to launch and display the message.

The programmatic interfaces of uiapplication allow-to-manage behavior that's 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, 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 abandon the Uiapplicationdelegate protocol and implement deploy some of the protocol Methods.

Subclassing Notes
Most apps should never need to subclass the UIApplication class. Most of the apps use the "an" app delegate to manage interactions between the system and the app.

The only situation where you might need to subclass UIApplication was when you were implementing a custom event or action di Spatching mechanism. In this situation, you might override the Sendevent:or sendAction:to:from:forEvent:methods to implement that mechanism. However, the cases where this is required was very rare and should be avoided whenever possible.


+ sharedapplication
Returns the Singleton app instance.

Declaration statement
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

Single-instance design pattern Step-by 003

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.