Application对象是应用程序的象征
每一个应用都有自己的UIApplication对象,而且是单例的
通过[UIApplication sharedApplication]可以获得这个单例对象一般用shared获得单例对象
一个iOS程序启动后创建的第一个对象就是UIApplication对象
创建一个单例
模仿苹果的做法
单例名字叫Person
语言OBJC
继承于NSObject
Person.h
#import <Foundation/Foundation.h>
@interface Person : NSObject
//获取单例
+ (instancetype)sharedPerson;
@end
Person.m
#import "Person.h"
@implementation Person
//程序启动的时候创建对象
//静态变量
static Person *_instance = nil;
//作用:加载类
//什么时候调用:每次程序一启动,就会把所有的类加载进内存
+ (void)load
{
NSLog(@"%s",__func__);
_instance = [[self alloc] init];
}
+ (instancetype)sharedPerson
{
return _instance;
}
+ (instancetype)alloc
{
if (_instance) {
//表示已经分配好了,就不允许外界再分配内存
//抛异常,告诉外界不运用分配
//创建异常类
//name:异常的名称
//reson:异常的原因
//userInfo:异常的信息
NSException *excp = [NSException exceptionWithName:@"NSInternalInconsistencyException" reason:@"There can only be one Person instance" userInfo:nil];
//抛异常
[excp raise];
}
//super -> NSObject 才知道怎么分配内存
//调用NSObject的做法
return [super alloc];
}
@end
常用属性
- 设置应用程序图标右上角的红色提醒数字
//ios10更新了推送
UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
UNAuthorizationOptions options = UNAuthorizationOptionBadge;
app.applicationIconBadgeNumber = 100;
[center requestAuthorizationWithOptions:options completionHandler:^(BOOL granted, NSError * _Nullable error) {
}];
- 设置一个联网指示器
app.networkActivityIndicatorVisible = YES;
- 设置状态栏
//隐藏状态栏,状态栏默认交给控制器管理
- (BOOL)prefersStatusBarHidden
{
return YES;
}
这里写图片描述
这里写图片描述
修改让APP管理状态栏
app.statusBarHidden = YES;
- openURL
NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"];
[app openURL:url options:nil completionHandler:^(BOOL success) {
}];
delegate
在APP收到干扰时,会产生系统事件,UIApplication会通知他的delegate对象,让代理来处理这些系统事件
//APPDelegate:监听应用程序的生命周期
// 应用程序启动完成的时候就会调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
return YES;
}
// 当应用程序失去焦点
- (void)applicationWillResignActive:(UIApplication *)application {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}
// 当应用程序进入后台的时候
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
//保存一些信息
}
// 即将进入前台的时候
- (void)applicationWillEnterForeground:(UIApplication *)application {
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}
// 当应用程序完全获取焦点的时候
- (void)applicationDidBecomeActive:(UIApplication *)application {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
// 程序关闭的时候
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
//保存一些信息
}
main.h中
int main(int argc, char * argv[]) {
@autoreleasepool {
// 第三个参数:UIApplication类名或者子类名称 nil == @"UIApplication"
// 第四个参数:UIApplication的代理的名称
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
UIApplication底层实现:
- 根据principalClassName传递的类名创建UIApplication对象
- 创建UIApplication代理对象,给UIApplication对象设置代理
- 开启主运行循环,处理事件,保持程序一直运行
- 加载info.plist,判断下是否制定main,如果指定main就回去加载
网友评论