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