UIApplication
UIApplication
UIApplication 類在整個 apps 運行過程中,扮演一個中央處理和協調的工作,任何程序應該只有一個 UIApplication 實例,當一個app 被推出時,系統會調用 UIApplicationMain 函數,在它的其它任務中,這個函數創建了一個單利 UIApplication 對象,此后,你可以調用 shareApplication 方法來獲取此對象。
此外,UIApplication 還處理著用戶的觸摸事件,分發 action message 到它擁有的合適的目標對象上。應用程序對象維持一個打開的窗口列表,通過這個列表可以檢索應用程序的任何 UIView 對象。
UIApplication 定義一個 delegate 對象,遵循 UIApplicationDelegate 協議,AppDelegate而且必須執行某些相關的協議方法。此外,應用程序對象通知這個代理對象一些很重要的運行時候的事件,比如app推出,內存緊張,app 終止等。
應用程序聯合處理一些資源,如 email, 圖片文件等,通過openURL:
方法,例如一個app調用 eamil url, 通過調用 open URL,可以喚醒 Mail app。
用UIApplication 相關的API可以管理設備的特定行為。如下所示:
暫停觸摸事件
beginIgnoringInteractionEvents
注冊遠程推送
unregisterForRemoteNotifications
觸發 undo-redo UI
applicationSupportsShakeToEdit
確定是否有一個安裝程序處理 URL scheme
canOpenURL:
擴展App應用程序的執行,以便它可以在后臺完成一個任務
beginBackgroundTaskWithExpirationHandler:, beginBackgroundTaskWithName:expirationHandler:
添加和取消本地的通知
scheduleLocalNotification:, cancelLocalNotification:
協調遙控接收事件
beginReceivingRemoteControlEvents, endReceivingRemoteControlEvents
執行app-level狀態恢復任務
methods in the Managing the State Restoration Behavior task group
獲得實例
+ (UIApplication *)sharedApplication
// 返回應用單個實例
獲得 app delegate
@property(nonatomic, assign) id< UIApplicationDelegate > delegate
// 這個應用的代理
獲得 app Windows
@property(nonatomic, readonly) UIWindow *keyWindow
// app的主 window
@property(nonatomic, readonly) NSArray <__kindof UIWindow *> *windows
// 隱藏的和看得見的所有 window
控制和處理事件
- (void)sendEvent:(UIEvent *)event
// 發送事件給app內適用的響應者
- (BOOL)sendAction:(SEL)action
to:(id)target
from:(id)sender
forEvent:(UIEvent *)event
// 發送一個含選擇器的動作消息到指定的目標
- (void)beginIgnoringInteractionEvents
// 告訴接受者暫停處理 touch相關的事件
- (void)endIgnoringInteractionEvents
// 告訴接受者繼續處理 touch相關的事件
- (BOOL)isIgnoringInteractionEvents
// 是否忽略交互事件
@property(nonatomic) BOOL applicationSupportsShakeToEdit
// 是否接受搖晃的時候, 展現 撤銷和恢復 視圖
打開 url 資源
- (BOOL)openURL:(NSURL *)url
// 通過特定的URL中打開資源
- (BOOL)canOpenURL:(NSURL *)url
// 返回一個bool值, 是否從已經安裝的 apps 中跳轉
配置用戶通知設置
- (void)registerUserNotificationSettings:(UIUserNotificationSettings *)notificationSettings
// 注冊可選的通知
- (UIUserNotificationSettings *)currentUserNotificationSettings
// 返回關于 user 對于這個app的通知設置
注冊遠程通知
- (void)registerForRemoteNotifications
// 注冊接受的遠程通知,這些通知經由 APNS 發出
- (void)unregisterForRemoteNotifications
// 注銷掉遠程通知
- (BOOL)isRegisteredForRemoteNotifications
// 表明是否已經注冊過了遠程通知
注冊本地通知
- (void)scheduleLocalNotification:(UILocalNotification *)notification
// 安排當地的本地通知,封裝了日期和時間
- (void)presentLocalNotificationNow:(UILocalNotification *)notification
// 立刻彈出本地通知
- (void)cancelLocalNotification:(UILocalNotification *)notification
// 取消預定的交付的本地通知
- (void)cancelAllLocalNotifications
// 取消所有的預定的本地通知
@property(nonatomic, copy) NSArray <UILocalNotification *> *scheduledLocalNotifications
// 目前的宿友的預定的本地通知
控制后臺擴展
@property(nonatomic, readonly) UIApplicationState applicationState
// app當前的運行的狀態
@property(nonatomic, readonly) NSTimeInterval backgroundTimeRemaining
// app 在后臺運行的時間
@property(nonatomic, readonly) UIBackgroundRefreshStatus backgroundRefreshStatus
// 進入到后臺,因此能夠進行后臺的操作
- (void)setMinimumBackgroundFetchInterval:(NSTimeInterval)minimumBackgroundFetchInterval
// 指定最小時間間隔在后臺獲取操作
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithName:(NSString *)taskName
expirationHandler:(void (^)(void))handler
// 標記新的長時間運行的任務以及指定任務的命名
- (UIBackgroundTaskIdentifier)beginBackgroundTaskWithExpirationHandler:(void (^)(void))handler
// 標記開始心的長時間運行的后臺任務
- (void)endBackgroundTask:(UIBackgroundTaskIdentifier)identifier
// 結束指定的長時間的后臺任務
@property(nonatomic, getter=isIdleTimerDisabled) BOOL idleTimerDisabled
// 一個bool值 確定控制器是否停止運行在空閑的時間
控制狀態恢復
- (void)extendStateRestoration
// 異步恢復狀態
- (void)completeStateRestoration
// 結束異步恢復狀態
- (void)ignoreSnapshotOnNextApplicationLaunch
// 阻止應用程序使用最近的快找圖像,在接下來的循環中
+ (void)registerObjectForStateRestoration:(id<UIStateRestoring>)object
restorationIdentifier:(NSString *)restorationIdentifier
// 注冊自定義對象的使用狀態恢復系統
控制 Home Screen 和 3D Touch
@property(nonatomic, copy) NSArray <UIApplicationShortcutItem *> *shortcutItems
// 重置此變量,設置一系列的 quick actions 用于3Dtouch展現
注冊遙控事件
- (void)beginReceivingRemoteControlEvents
// 告訴app 開始接受遙控事件
- (void)endReceivingRemoteControlEvents
// 告訴app 結束接受遙控事件
控制應用程序外觀
@property(nonatomic, readonly) CGRect statusBarFrame
// 獲取狀態欄的 rect
@property(nonatomic, getter=isNetworkActivityIndicatorVisible) BOOL networkActivityIndicatorVisible
// 是否指示網絡活動,默認是NO
@property(nonatomic) NSInteger applicationIconBadgeNumber
// 未讀消息數字
@property(nonatomic, readonly) UIUserInterfaceLayoutDirection userInterfaceLayoutDirection
// 返回用戶界面的布局方向。
控制默認的方向
- (UIInterfaceOrientationMask)supportedInterfaceOrientationsForWindow:(UIWindow *)window
// 在指定的窗口中, 返回默認的視圖控制器方向接口
控制狀態欄方向
@property(nonatomic, readonly) NSTimeInterval statusBarOrientationAnimationDuration
// 狀態欄動畫持續時間
字體大小偏好
@property(nonatomic, readonly) NSString *preferredContentSizeCategory
// 字體偏好
數據類型
UIBackgroundTaskIdentifier;
// 一個獨特的標志,這個標志用于在后臺請求運行
UIRemoteNotificationType
// 指示應用程序的通知類型
typedef enum : NSUInteger {
UIRemoteNotificationTypeNone = 0,
UIRemoteNotificationTypeBadge = 1 << 0,
UIRemoteNotificationTypeSound = 1 << 1,
UIRemoteNotificationTypeAlert = 1 << 2,
UIRemoteNotificationTypeNewsstandContentAvailability = 1 << 3
} UIRemoteNotificationType;
UIStatusBarStyle
// 狀態欄的樣式
typedef enum : NSInteger {
UIStatusBarStyleDefault, // 黑色狀態,用于白背景
UIStatusBarStyleLightContent, // light 狀態, 用于dark 背景
UIStatusBarStyleBlackTranslucent, // 7.0 棄用
UIStatusBarStyleBlackOpaque // 7.0 棄用
} UIStatusBarStyle;
UIStatusBarAnimation
// 在狀態欄隱藏于顯現之間的動畫
typedef enum : NSInteger {
UIStatusBarAnimationNone,
UIStatusBarAnimationFade,
UIStatusBarAnimationSlide,
} UIStatusBarAnimation;
常量(Constants)
UIApplicationState // 應用狀態
typedef enum : NSInteger {
UIApplicationStateActive,
UIApplicationStateInactive,
UIApplicationStateBackground
} UIApplicationState;
在后臺運行時候常用的常量
const UIBackgroundTaskIdentifier UIBackgroundTaskInvalid;
// 無效的后臺任務
const NSTimeInterval UIMinimumKeepAliveTimeout;
// 后臺保持的最小的時間
UIBackgroundFetchResult
// 后臺請求結果
typedef enum : NSUInteger {
UIBackgroundFetchResultNewData,
UIBackgroundFetchResultNoData,
UIBackgroundFetchResultFailed
} UIBackgroundFetchResult;
Fetch Intervals
// 請求間隔
const NSTimeInterval UIApplicationBackgroundFetchIntervalMinimum; // 系統支持的最小的間隔
const NSTimeInterval UIApplicationBackgroundFetchIntervalNever; // 最大間隔, 以阻止請求
UIBackgroundRefreshStatus
typedef enum : NSUInteger {
UIBackgroundRefreshStatusRestricted, // 限制
UIBackgroundRefreshStatusDenied, // 否定
UIBackgroundRefreshStatusAvailable // 可用
} UIBackgroundRefreshStatus;
UIInterfaceOrientation
// 應用程序的用戶界面方向
typedef enum : NSInteger {
UIInterfaceOrientationUnknown = UIDeviceOrientationUnknown,
UIInterfaceOrientationPortrait = UIDeviceOrientationPortrait,
UIInterfaceOrientationPortraitUpsideDown = UIDeviceOrientationPortraitUpsideDown,
UIInterfaceOrientationLandscapeLeft = UIDeviceOrientationLandscapeRight,
UIInterfaceOrientationLandscapeRight = UIDeviceOrientationLandscapeLeft
} UIInterfaceOrientation;
UIInterfaceOrientationMask
// 指定一個視圖控制器支持的接口方向。
typedef enum : NSUInteger {
UIInterfaceOrientationMaskPortrait = (1 << UIInterfaceOrientationPortrait ),
UIInterfaceOrientationMaskLandscapeLeft = (1 << UIInterfaceOrientationLandscapeLeft ),
UIInterfaceOrientationMaskLandscapeRight = (1 << UIInterfaceOrientationLandscapeRight ),
UIInterfaceOrientationMaskPortraitUpsideDown = (1 << UIInterfaceOrientationPortraitUpsideDown ),
UIInterfaceOrientationMaskLandscape = (UIInterfaceOrientationMaskLandscapeLeft | UIInterfaceOrientationMaskLandscapeRight ),
UIInterfaceOrientationMaskAll = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight | UIInterfaceOrientationMaskPortraitUpsideDown ),
UIInterfaceOrientationMaskAllButUpsideDown = (UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskLandscapeLeft |
UIInterfaceOrientationMaskLandscapeRight ),
} UIInterfaceOrientationMask;
UserInfo Dictionary Keys
// 這些鍵值用來接受用戶信息字典,用于訪問值一些UIApplication-posted通知。
NSString *const UIApplicationStatusBarOrientationUserInfoKey;
NSString *const UIApplicationStatusBarFrameUserInfoKey;
Content Size Category Constants // 內容大笑類別常量
NSString *const UIContentSizeCategoryExtraSmall;
NSString *const UIContentSizeCategorySmall;
NSString *const UIContentSizeCategoryMedium;
NSString *const UIContentSizeCategoryLarge;
NSString *const UIContentSizeCategoryExtraLarge;
NSString *const UIContentSizeCategoryExtraExtraLarge;
NSString *const UIContentSizeCategoryExtraExtraExtraLarge;
// 內容尺寸改變的通知的key
NSString *const UIContentSizeCategoryNewValueKey;
通知
UIApplicationBackgroundRefreshStatusDidChangeNotification
// 在后臺下載內容的應用程序的狀態變化時候通知
UIApplicationDidBecomeActiveNotification
// 當程序變的活躍之后
UIApplicationDidChangeStatusBarFrameNotification
// 當狀態欄frame 改變時候
UIApplicationDidChangeStatusBarOrientationNotification
// 當用戶方向改變時候
UIApplicationDidEnterBackgroundNotification
// 當app已經進入后臺之后
UIApplicationDidFinishLaunchingNotification
// 當app完全推出之后
UIApplicationDidReceiveMemoryWarningNotification
// 當應用內存緊張之后
UIApplicationProtectedDataDidBecomeAvailable
// 但受保護的文件進入活躍狀態
UIApplicationProtectedDataWillBecomeUnavailable
// 當被保護的文件進入不活躍狀態
UIApplicationUserDidTakeScreenshotNotification
// 當截屏的時候
UIApplicationWillChangeStatusBarOrientationNotification
// 當應用程序將要改變其接口方向
UIApplicationWillChangeStatusBarFrameNotification
// 當應用將要改變狀態來frame
UIApplicationWillEnterForegroundNotification
// 當應用程序從后臺將要進入前臺
UIApplicationWillResignActiveNotification
// 應用程序不再主動和失去焦點。
UIApplicationWillTerminateNotification
// 當應用程序將要終止。
UIContentSizeCategoryDidChangeNotification
// 當用戶更改內容大小的偏好設置