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阅读 194,390评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,821评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,632评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,170评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,033评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,098评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,511评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,204评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,479评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,572评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,341评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,213评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,576评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,893评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,171评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,486评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,676评论 2 335

推荐阅读更多精彩内容