UIApplication

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
 // 當用戶更改內容大小的偏好設置
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,488評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,034評論 3 414
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,327評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,554評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,337評論 6 404
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,883評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 42,975評論 3 439
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,114評論 0 286
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,625評論 1 332
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,555評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,737評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,244評論 5 355
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 43,973評論 3 345
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,362評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,615評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,343評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,699評論 2 370

推薦閱讀更多精彩內容