1 前言
IOS4 之后提供了后台处理,在后台运行应用程序,在一些情形下甚至可以在用户按下Home按钮之后在后台运行。

2 详述
IOS可以在用户按下Home按钮后将应用程序添加到暂停状态。这种暂停执行的状态在概念上类似于将Mac设置为休眠模式。应用程序的所有工作内存都在RAM中,在暂停时它完全不执行。因此,切换回这样的应用程序的速度非常快。系统提供了多种方式,通过UIApplication类向应用程序通知其执行状态的变化,该类针对此用途提供了许多委托方法和通知,我们将介绍如何使用他们。

2.1 应用程序的声明周期
2.1.1 未运行
此状态表明所有应用程序都位于一个刚刚重启的设备上,在设备打开状态下,不论应用程序在何时启动,只有遇到以下情况应用程序才返回未运行状态:

(1)应用程序的Info.plist包含UIApplicationExitsOnSuspend键设置为YES;

(2)应用程序之前被暂停并且系统需要清楚一些内存;

(3)应用程序在运行过程中崩溃。

2.1.2 活动
这是应用程序在屏幕上显示时的正常运行状态。他可以接收用户输入并更新显示。

2.1.3 后台
此状态中,应用程序获得了一定的时间来执行一些代码,但是它无法直接访问屏幕或者获得任何用户输入。在用户按下Home按钮后不久,所有应用程序都会进入状态,他们中的大部分会迅速进入暂停状态。希望在后台运行的应用程序一直处于此状态,直到被在此激活。

2.1.4 暂停
暂停的应用程序被冻结。普通的应用程序处于后台不久会转变为此状态。此应用程序在活动时使用的所有内存将原封不懂的得以保留。如果用户将应用程序切换回活动状态,它将回复到之前的状态。另一方面,如果系统需要为当前活动的应用程序提供更多的内存,任何暂停的应用程序都可能被冻结(并返回到未运行状态),他们的内存将释放用于其他用途。

2.1.5 不活动
应用程序仅在两个其他状态之间的临界过度阶段处于不活动状态。应用程序可以在任意长的时间内处于不活动状态的唯一前提是,用户正在处理系统提示(比如显示的传入呼叫或者SMS提示)或用户锁定了屏幕。这基本时上是一种中间的过度状态。

2.2 状态更改通知
为了管理这些状态之间的更改,UIApplication定义了它的委托可以实现的一些方法。除了委托方法,UIApplication还定义了一个匹配的通知名称集合。这使得除了应用程序委托外的其他对象可以在应用程序状态更改时注册通知。

跟踪应用程序的执行状态和相应的通知名称的委托方法:

委托方法通知名称

//可以在application:didFinishLaunchingWithOptions:添加一些应用程序初始化代码。

application:didFinishLaunchingWithOptions:UIApplicationDidFinishLaunchingNotification

//如果按下home按钮,将调用applicationWillResignActive,不应该在applicationWillResignActive中假设应用程序将要进入后台状态,它只是一种临界状态。最终将恢复到活动状态。

applicationWillResignActive:UIApplicationWillResignActiveNotification

//如果稍后将应用程序切回到前台将调用applicationDidBecomeActive

applicationDidBecomeActive:UIApplicationDidBecomeActiveNotification

//释放所有有可能在以后重新创建的资源,保存所有用户数据,关闭网络连接等。如果在这里花了太长时间(超过5秒),系统将断定应用程序的行为异常并终止它。其应该实现applicationWillEnterForeground:来重新创建在applicationDidEnterBackground:中销毁的内容。

applicationDidEnterBackground:UIApplicationDidEnterBackgroundNotification

applicationWillEnterForeground:UIApplicationWillEnterForegroundNotification

//很少用这个方法,只有在应用程序进入后台,并且系统处于某种原因决定跳过暂停状态并终止应用程序时,才会真正调用它。

applicationWillTerminate:UIApplicationWillTerminateNotification

2.3实例解析
接下来我们建立一个项目,来真正的观察你一下这些方法的调用时间:

ZYAppDelegate.m:

[plain]
// 
//  ZYAppDelegate.m 
//  State Lab 
// 
//  Created by zhangyuc on 13-6-8. 
//  Copyright (c) 2013年 zhangyuc. All rights reserved. 
// 
 
#import "ZYAppDelegate.h" 
 
#import "ZYViewController.h" 
 
@implementation ZYAppDelegate 
 
- (void)dealloc 

    [_window release]; 
    [_viewController release]; 
    [super dealloc]; 

 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; 
    /** 
     *_cmd:Objective-C提供了一个方便的内置变量,名为_cmd,它始终包含当前方法的选择器。 
     *NSStringFromSelector() :函数返回给制定选择器的NSString表示 
     *二者结合可以提供输出当前方法名称的快捷方式,无需重新键入或者复制黏贴它。 
     */ 
    NSLog(@"%@",NSStringFromSelector(_cmd)); 
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease]; 
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible]; 
    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 throttle down OpenGL ES frame rates. Games should use this method to pause the game. 
     NSLog(@"%@",NSStringFromSelector(_cmd)); 

 
- (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(@"%@",NSStringFromSelector(_cmd)); 

 
- (void)applicationWillEnterForeground:(UIApplication *)application 

    // 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. 
     NSLog(@"%@",NSStringFromSelector(_cmd)); 

 
- (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(@"%@",NSStringFromSelector(_cmd)); 

 
- (void)applicationWillTerminate:(UIApplication *)application 

    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. 
     NSLog(@"%@",NSStringFromSelector(_cmd)); 

 
@end

//
//  ZYAppDelegate.m
//  State Lab
//
//  Created by zhangyuc on 13-6-8.
//  Copyright (c) 2013年 zhangyuc. All rights reserved.
//

#import "ZYAppDelegate.h"

#import "ZYViewController.h"

@implementation ZYAppDelegate

- (void)dealloc
{
    [_window release];
    [_viewController release];
    [super dealloc];
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    /**
     *_cmd:Objective-C提供了一个方便的内置变量,名为_cmd,它始终包含当前方法的选择器。
     *NSStringFromSelector() :函数返回给制定选择器的NSString表示
     *二者结合可以提供输出当前方法名称的快捷方式,无需重新键入或者复制黏贴它。
     */
    NSLog(@"%@",NSStringFromSelector(_cmd));
    self.viewController = [[[ZYViewController alloc] initWithNibName:@"ZYViewController" bundle:nil] autorelease];
    self.window.rootViewController = self.viewController;
    [self.window makeKeyAndVisible];
    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 throttle down OpenGL ES frame rates. Games should use this method to pause the game.
     NSLog(@"%@",NSStringFromSelector(_cmd));
}

- (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(@"%@",NSStringFromSelector(_cmd));
}

- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // 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.
     NSLog(@"%@",NSStringFromSelector(_cmd));
}

- (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(@"%@",NSStringFromSelector(_cmd));
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
     NSLog(@"%@",NSStringFromSelector(_cmd));
}

@end
运行结果(控制台):

刚进入程序:

2013-06-08 10:29:17.243 State Lab[1866:c07] application:didFinishLaunchingWithOptions:

2013-06-08 10:29:17.248 State Lab[1866:c07] applicationDidBecomeActive:

点击Home键:
再重新回到项目:
最后一种情况很有趣,先将程序迅速地在此激活,然后变为不活动,最后进入后台。
这些委托方法和通知都直接与某种“运行”状态有关:活动,不活动和后台。每个委托方法仅在一种状态中调用(每个通知也仅在一种状态中出现)。最重要的状态过度是在活动状态与其他状态之间,一些过度(比如从后台到暂停)不会出现任何通知。

2013-06-08 10:29:48.598 State Lab[1866:c07] applicationWillResignActive:

2013-06-08 10:29:48.599 State Lab[1866:c07] applicationDidEnterBackground:

2013-06-08 10:30:11.357 State Lab[1866:c07] applicationWillEnterForeground:

2013-06-08 10:30:11.358 State Lab[1866:c07] applicationDidBecomeActive:

如果手机收到一条SMS消息:

2013-06-08 10:29:48.598 State Lab[1866:c07] applicationWillResignActive:

如果关闭该消息:

2013-06-08 10:29:17.248 State Lab[1866:c07] applicationDidBecomeActive:

如果回复SMS消息:

2013-06-08 10:29:17.248 State Lab[1866:c07] applicationDidBecomeActive:

2013-06-08 10:29:48.598 State Lab[1866:c07] applicationWillResignActive:

2013-06-08 10:29:48.599 State Lab[1866:c07] applicationDidEnterBackground:

IOS开发之后台处理的更多相关文章

  1. ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)

    ABBPlayerKit ios开发视频播放后台下载功能实现 : 代码下载地址:https://github.com/niexiaobo/ABBPlayerKit github资料学习和下载地址:ht ...

  2. iOS开发:后台运行以及保持程序在后台长时间运行

    第一部分 1.先说说iOS 应用程序5个状态: 停止运行-应用程序已经终止,或者还未启动. 不活动-应用程序处于前台但不再接收事件(例如,用户在app处于活动时锁住了设备). 活动-app处于“使用中 ...

  3. iOS开发- 蓝牙后台接收数据(BLE4.0)

    最近在做一个蓝牙相关的项目, 需要在应用进入后台, 或者手机属于锁屏状态的情况下, 仍然保持蓝牙连接, 并且能正常接收数据. 本来以后会很麻烦, 但是学习了下..发现就2步而已.简单的不能再简单了. ...

  4. iOS开发小技巧--iOS程序进入后台运行的实现

    iOS程序进入后台运行的实现 视频中看到老师用的iOS7,代码中有开启timer,无限请求数据的功能,但是切换到后台,代码就不打印了 自己用的iOS9,进入后台还是可以打印的,再次进入前台也可以正常运 ...

  5. iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位

    1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...

  6. iOS开发系列--App扩展开发

    概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...

  7. iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总

    --系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...

  8. iOS开发系列--通知与消息机制

    概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...

  9. iOS开发系列--地图与定位

    概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...

随机推荐

  1. spark-sql启动后在监控页面中显示的Application Name为SparkSQL::xxxx的疑问

    启动spark-sql执行sql时,在监控页面中看到该Application的Name是SparkSQL:hadoop000(其中hadoop000是测试机器的hostname),就有个想法,修改下该 ...

  2. MFC学习 多线程

    #include <Windows.h> #include <process.h> #include <stdio.h> HANDLE hMutex; //互斥对象 ...

  3. Unity Shader : Ghost(残影) v1

    前阵子组长给我提了个需求,要实现角色人物的残影.我百度google了一下,发现可以用两种方式实现这个效果:1.记录前几帧的人物位置,将其传入shader中,对每个位置进行一个pass渲染.2. 通过相 ...

  4. PostMessage与SendMessage的区别(二)

    在做基于窗口的Windows程序的时候,我们避免不了要向窗口发送消息,有两种方式,一种是PostMessage,另外一种是SendMessage.关于这两个宏,我是通过狠狠的看MSDN才搞明白的,那里 ...

  5. 微信公众号发起微信支付 c#

    tenpay.dll: MD5Util.cs using System; using System.Collections.Generic; using System.Linq; using Syst ...

  6. 在 C# 控制台中记录异常日志并输出

    最近做了一个小程序,要求在控制台中记录程序运行的异常并输出到指定的文件夹中,以下是我的具体的程序代码: public static void ErrorLog(Exception ex) { stri ...

  7. 学习练习 java 实例属性 静态属性

    package com.hanqi; public class Test11Car11 { //静态 //实例属性 private int m = 0; //静态属性 //所有实例共有的,在内存里只有 ...

  8. mongodb replica set(副本集)设置步骤

    网上已经有一大堆的设置步骤的了,根据我遇到的问题,整理一下,如下: 首先先去下载一个mongodb最新版,目前最新版应该是2.6 cd /usr/local/bin wget http://fastd ...

  9. 【MySQL】查询使用临时表

    MySQL查询产生临时表的分析 官网说明的地址:http://dev.mysql.com/doc/refman/5.5/en/internal-temporary-tables.html 参考:htt ...

  10. MyEclipse 中文乱码 史诗级解决方法。也可用于其他编码

    最近发现以前写的项目全乱码了.唯独  .java 中的中文全是乱码. 由于,后期的项目把默认编码改成了UTF-8所以就乱了. 每个编码表的编码都不一样.不能直接通过改某个属性来更改达到目的 (除非你是 ...