UIApplication Delegate消息
一.UIApplication Delegate消息顺序
iOS4之后引入了多任务(multitasking),UIApplication有前台(Foreground)和后台(Background)运行两种模式,在APP启动以及前后台之间切换时会调用UIApplicaton的Delegate方法,下图描述了各种情况下的Delegate消息。
对应的PDF文件。
在UIApplication的状态转换过程中根据不同的情况会发送不同的Delegate消息,下面主要描述UIApplication状态转换的各种情况以及发送哪些Delegate消息及消息的顺。
首先将涉及到UIApplication状态转换的Delegate消息编号如下:
- application:didFinishLaunchingWithOptions:
- application:openURL:sourceApplication:annotation:
- applicationDidBecomeActive:
- applicationWillResignActive:
- applicationDidEnterBackground:
- applicationWillEnterForeground:
- applicationWillTerminate:
则对于下面各种操作Delegate消息的发送顺序为
- 通过SpringBoard第一次启动,消息顺序为1-3
- 通过URL第一次启动,消息顺序为1-2-3
- 运行过程中按Home键,消息顺序为 4-5
- 若为iOS4.0以下或者Info.plist中设置了UIApplicationExitsOnSuspend,则继续发送消息 7,然后程序终止;
- 否则根据程序设置进入Background运行模式或者Suspend状态。
- 运行过程中按锁屏键,消息顺序与按Home键相同,区别在于此时会关闭所有网络连接(iOS5.0+)
- 运行过程中被短信/电话等中断,则发送消息 4
- 若用户选择短信/电话,则发送消息 5,后续流程与按Home键相同。
- 若用户忽略短信/电话,则发送消息 3,回到Foreground运行状态。
- 运行过程中双击Home键显示任务切换器,会发送消息 4
- 若选择其他程序或者单击Home键返回到SpringBoard,则发送消息 5,后续流程与按Home键相同。
- 若再次双击Home键返回APP,则发送消息 3,回到Foreground运行状态。
- 通过SpringBoard或者任务切换器再次启动时,消息顺序为6-3.
从后台重新返回前台的转换过程中,即6与3消息的中间,在5.1的模拟器中显示的是Default.png,但在真实设备上显示的进入后台的screenshot。 - 通过URL再次启动时,消息顺序6-2-3.
- 程序在Background/Suspend状态时,用户双击Home键,在任务切换器中删除此程序时,APP会立即终止,不会有任何消息发送。
- 当程序在Suspend状态时,若内存紧张,系统也会将程序立即终止以回收内存,此时也不会有任何消息发送。
二.状态转换广播通知
以上消息只会发给AppDelegate,如果想在UIViewController中监听UIApplication的状态变化,可以监听以下通知
UIApplicationDidBecomeActiveNotification (iOS 2.0+)
UIApplicationDidEnterBackgroundNotification (iOS 4.0+)
UIApplicationWillEnterForegroundNotification (iOS 4.0+)
UIApplicationWillResignActiveNotification (iOS 2.0+)
UIApplicationWillTerminateNotification (iOS 2.0+)
使用下面代码可获取当前Application的运行状态
[UIApplication sharedApplication].applicationState
状态包括
UIApplicationStateActive
UIApplicationStateInactive
UIApplicationStateBackground
当APP在某个UIViewController显示时进入后台或者从后台返回到前台时,并不会调用viewWillDisappear:,viewDidDisappear:,viewWillAppear:,viewDidAppear:系列方法,但此时的视图确实发生Disappear以及Appear的事件。
如果在这种情况下需要做些处理的话,可以在viewDidAppear:中注册对上述通知的监听,在接收到APP进入后台以及从后台返回时通知时做相应的处理。在viewWillDisappear:中我们可以取消对上述通知的监听,因为如果当前显示的不是此视图,就没有Disappear以及Appear的事件发生,也就不需要在前后台切换时做任何特殊处理。
需要这样做的情况有:
- 在View Appear时需要重新请求数据时;
- 当视图包含动画,在进入后台模式时动画会停止,返回到前台时需要重新启动动画时;
- …
三.CoreLocation 更新消息
如果使用CoreLocation,当程序运行在Background模式下出现位置更新时,还有特殊的消息需要处理,见CoreLocation Background Update Messaging。
参考:
Understanding iOS 4 Backgrounding and Delegate Messaging
CoreLocation Background Update Messaging
Reload application data when the app comes into foreground?
IOS Default.png show every time when my app enter foreground from background
iOS how to judge application is running foreground or background?
iphone app network connection disconnect after screen locking with new ios sdk 5.0
Locking iPhone disconnects sockets on iOS 5 only
UIApplicationDelegate Protocol Reference
UIApplication Class Reference
转载-- 清风徐来,水波不兴 的博客 http://www.winddisk.com/2012/08/29/uiapplication-delegate-messagin/
对于需要在程序退出时候做处理的很有帮助,但是文中说法有误,更正下。
一.UIApplication Delegate消息顺序中
9.程序在Background/Suspend状态时,用户双击Home键,在任务切换器中删除此程序时,APP会立即终止,不会有任何消息发送。
此说法不正确,应该区分情况。在IOS4.0以后由于支持多任务,程序可以run in background,切默认该选项是开启的。a。如果程序同时启用了Required background modes,比如App plays audio,此时如果程序home到后台,并且正在播放音乐,此时用户手动/自动中止程序是会触发 applicationWillTerminate:的。如果你使用了后台任务处理beginBackgroundTaskWithExpirationHandler也是同样道理。b。如果程序在后台时app并没有后台任务执行,则会进入Suspend,此时手动/自动中止程序则不会触发 applicationWillTerminate: 的。总结:1.程序在Suspend状态被中止时不会触发 applicationWillTerminate:2.程序在Background状态并且有后台任务执行时被中止则会触发applicationWillTerminate:所以对于需要在程序退出时保存数据,状态等的需求,最好在applicationDidEnterBackground:以及applicationWillTerminate:例都做处理。