UIApplication

来源:互联网 发布:网络安全教育简报 编辑:程序博客网 时间:2024/06/18 15:34

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;   // 最大间隔, 以阻止请求UIBackgroundRefreshStatustypedef 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 // 当用户更改内容大小的偏好设置


链接:http://www.jianshu.com/p/597b9d108f39