UIApplication和delegate代理
程序员文章站
2022-07-04 12:02:47
...
所有的移动操作系统都有个致命的缺点:app很容易受到打扰。比如一个来电或者锁屏会导致app进入后台甚至被终止
还有很多其它类似的情况会导致app受到干扰,在app受到干扰时,会产生一些系统事件,这时UIApplication会通知它的delegate对象,让delegate代理来处理这些系统事件
delegate可处理的事件包括:
应用程序的生命周期事件(如程序启动和关闭)
系统事件(如来电)
内存警告
…每次新建完项目,都有个带有“AppDelegate”字眼的类,它就是UIApplication的代理
AppDelegate默认已经遵守了UIApplicationDelegate协议,已经是UIApplication的代理
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
- 主要的方法有
//应用程序启动完毕时调用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
NSLog(@"%s",__func__);
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.
NSLog(@"%s",__func__);
}
//应用程序进入到后台时调用
- (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.
NSLog(@"%s",__func__);
}
//应用程序进入到前台时调用
- (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.
NSLog(@"%s",__func__);
}
//应用程序获取焦点
//焦点:能否与用户进行交互.
- (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.
NSLog(@"%s",__func__);
}
//当应用程序退出的时候调用
- (void)applicationWillTerminate:(UIApplication *)application {
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
NSLog(@"%s",__func__);
}
//当应用程序收到内存警告时调用
-(void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
//清理缓存.图片,视频.
NSLog(@"%s",__func__);
}
上一篇: vue使用axios实现异步请求
推荐阅读
-
nginx配置反向代理和负载均衡(nginx功能介绍和使用)
-
代理方式有几种(一般代理和全权代理的区别)
-
门面模式和代理模式区别(2种模式对比分析)
-
门面模式和代理模式区别(2种模式对比分析)
-
详解Spring的两种代理方式:JDK动态代理和CGLIB动态代理
-
极光大数据:B站月均DAU超2,000万,游戏代理和联运或成盈利关键
-
.net core3.0部署Linux服务器 使用Docker容器和Nginx反代理教程
-
Spring学习之动态代理(JDK动态代理和CGLIB动态代理)
-
Mybaits 源码解析 (十一)----- 设计模式精妙使用:静态代理和动态代理结合使用:@MapperScan将Mapper接口生成代理注入到Spring
-
IOS开发(45)之delegate和Notification的区别