本篇文章主要介绍一些UIApplicationDelegate中几个常用的回调方法的调用时机。
以帮助你判断哪些方法倒底放到哪个回调中去实现。
1. – (void)applicationDidFinishLaunching:(UIApplication *)application;
此方法基本已经弃用,改用第2个方法代替。
2. – (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions NS_AVAILABLE_IOS(3_0);
当应用程序启动时(不包括已在后台的情况下转到前台),调用此回调。launchOptions是启动参数,假如用户通过点击push通知启动的应用,这个参数里会存储一些push通知的信息。
3. – (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.
当应用程序全新启动,或者在后台转到前台,完全激活时,都会调用这个方法。如果应用程序是以前运行在后台,这时可以选择刷新用户界面。
4. – (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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
当应用从活动状态主动到非活动状态的应用程序时会调用这个方法。这可导致产生某些类型的临时中断(如传入电话呼叫或SMS消息)。或者当用户退出应用程 序,它开始过渡到的背景状态。使用此方法可以暂停正在进行的任务,禁用定时器,降低OpenGL ES的帧速率。游戏应该使用这种方法来暂停游戏。
调用时机可能有以下几种:锁屏,按HOME键,下接状态栏,双击HOME键弹出低栏,等情况。
5. – (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url;
// Will be deprecated at some point, please replace with application:openURL:sourceApplication:annotation:
这个方法已不再支持,可能会在以后某个版本中去掉。建议用下面第6个方法代替
6. – (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation NS_AVAILABLE_IOS(4_2);
// no equiv. notification. return NO if the application can’t open for some reason
当用户通过其它应用启动本应用时,会回调这个方法,url参数是其它应用调用openURL:方法时传过来的。
7. – (void)applicationDidReceiveMemoryWarning:(UIApplication *)application;
// try to clean up as much memory as possible. next step is to terminate app
当应用可用内存不足时,会调用此方法,在这个方法中,应该尽量去清理可能释放的内存。如果实在不行,可能会被强行退出应用。
8. – (void)applicationWillTerminate:(UIApplication *)application;
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
当应用退出,并且进程即将结束时会调到这个方法,一般很少主动调到,更多是内存不足时是被迫调到的,我们应该在这个方法里做一些数据存储操作。
9. // one of these will be called after calling -registerForRemoteNotifications
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken NS_AVAILABLE_IOS(3_0);
- (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error NS_AVAILABLE_IOS(3_0);
当客户端注册远程通知时,会回调上面两个方法。
如果成功,则回调第一个,客户端把deviceToken取出来发给服务端,push消息的时候要用。
如果失败了,则回调第二个,可以从error参数中看一下失败原因。
注:注册远程通知使用如下方法:
UIRemoteNotificationType t=UIRemoteNotificationTypeBadge|UIRemoteNotificationTypeAlert|UIRemoteNotificationTypeSound; [[UIApplication sharedApplication] registerForRemoteNotificationTypes:t];
10. – (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo NS_AVAILABLE_IOS(3_0);
当应用在前台运行中,收到远程通知时,会回调这个方法。
当应用在后台状态时,点击push消息启动应用,也会回调这个方法。
11. – (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification NS_AVAILABLE_IOS(4_0);
当应用收到本地通知时会调这个方法,同上面一个方法类似。
如果在前台运行状态直接调用,如果在后台状态,点击通知启动时,也会回调这个方法
本地通知可见另一篇文章:http://bluevt.org/?p=70
12. – (void)applicationDidEnterBackground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
// 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.
当用户从台前状态转入后台时,调用此方法。使用此方法来释放资源共享,保存用户数据,无效计时器,并储存足够的应用程序状态信息的情况下被终止后,将应用 程序恢复到目前的状态。如果您的应用程序支持后台运行,这种方法被调用,否则调用applicationWillTerminate:用户退出。
13. – (void)applicationWillEnterForeground:(UIApplication *)application NS_AVAILABLE_IOS(4_0);
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
当应用在后台状态,将要进行动前台运行状态时,会调用此方法。
如果应用不在后台状态,而是直接启动,则不会回调此方法。
相关推荐
项目中AppDelegate的用法,主要介绍AppDelegate.h和AppDelegate.m用法的介绍。
在AppDelegate类中轻松定义全局变量,几乎和C语言一样来定义。
(Swift)AppDelegate中启动UI
(OC)AppDelegate中启动UI
代理协议
无代码入侵,获取AppDelegate声明周期
使用场景加载scn文件(scn格式文件是一个基于3D建模的文件,使用3DMax软件可以创建,这里系统有一个默认的3D飞机)--------在右侧我添加了许多3D模型,只需要替换文件名即可 SCNScene *scene = [SCNScene ...
SwiftUI - AppDelegate
iOS开的常见8中传值(初始化传值,代理传值,属性传值,代理传值,block传值,通知传值,NSuserDefault传值,AppDelegate传值)
使用通知和runtime-AOP的为AppDelegate瘦身方案
PluggableApplicationDelegate, 使用基于分离的服务架构的最小 AppDelegate PluggableApplicationDelegate 简介AppDelegate 是一个传统的错误代码示例。 在应用程序生命周期中调用的方法中将许多不同的代码组合在...
1.在AppDelegate中定义的视图内容随旋屏而自动转换 2.从一个Controller中跳转到另一个Controller
至于本地通知,iOS10之前采用的是UILocationNotification类,远程通知有苹果服务器进行转发,本地通知和远程通知其回调的处理都是通过AppDelegate中的几个回调方法来完成。iOS10系统中,通知功能的增强是一大优化之...
用php给app发通知及AppDelegate所用部分代码
如果是iOS 9.0之前,处理微信回调时,首先在APPDelegate中写如下的代码: //iOS 9.0 之前的处理方法不保证正确,如有错误还望指正 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url source...
WKJNetworking ...可以根据公司的业务逻辑及接口的数据结构自定义响应回调的调用时机以及返回值,在AppDelegate(保证在所有请求开始之前调用即可)添加以下方法: /* 某接口返回数据格式如下: * { * "co
iphone中的基础控件UINavigationController,适合初学者,主要学习UINavigationController的基本属性和常用方法调用。 UINavigationController也是一种常用的容器,跟前边学过的tabbar差不多,在这个容器中可以添加...
-[AppDelegate setWindow:]: unrecognized selector sent to instance 0x60000002b440 修改方法: 在AppDelegate.h里加声明window @property (nonatomic, strong) UIWindow * window; 运行问题解决 兼容13和13以前...
这是一个键盘监听第三方库 特点: 1.特别简单 ,直接将IQKeyboardManager文件夹拉到项目中就行了 2.如果要做修改,可以在AppDelegate中直接修改 主要用的一些属性: *可以在指定的视图中用 也可以直接在...