IOS开发之后台处理
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开发之后台处理的更多相关文章
- ios开发视频播放后台下载功能实现 :1,ios播放视频 ,包含基于AVPlayer播放器,2,实现下载,iOS后台下载(多任务同时下载,单任务下载,下载进度,下载百分比,文件大小,下载状态)(真机调试功能正常)
ABBPlayerKit ios开发视频播放后台下载功能实现 : 代码下载地址:https://github.com/niexiaobo/ABBPlayerKit github资料学习和下载地址:ht ...
- iOS开发:后台运行以及保持程序在后台长时间运行
第一部分 1.先说说iOS 应用程序5个状态: 停止运行-应用程序已经终止,或者还未启动. 不活动-应用程序处于前台但不再接收事件(例如,用户在app处于活动时锁住了设备). 活动-app处于“使用中 ...
- iOS开发- 蓝牙后台接收数据(BLE4.0)
最近在做一个蓝牙相关的项目, 需要在应用进入后台, 或者手机属于锁屏状态的情况下, 仍然保持蓝牙连接, 并且能正常接收数据. 本来以后会很麻烦, 但是学习了下..发现就2步而已.简单的不能再简单了. ...
- iOS开发小技巧--iOS程序进入后台运行的实现
iOS程序进入后台运行的实现 视频中看到老师用的iOS7,代码中有开启timer,无限请求数据的功能,但是切换到后台,代码就不打印了 自己用的iOS9,进入后台还是可以打印的,再次进入前台也可以正常运 ...
- iOS开发雕虫小技之傻瓜式定位神器-超简单方式解决iOS后台定时定位
1.概述 由于公司一款产品的需求,最近一直在研究iOS设备的后台定位.主要的难点就是,当系统进入后台之后,程序会被挂起,届时定时器.以及代码都不会Run~ 所以一旦用户将我的App先换到了后台,我的定 ...
- iOS开发系列--App扩展开发
概述 从iOS 8 开始Apple引入了扩展(Extension)用于增强系统应用服务和应用之间的交互.它的出现让自定义键盘.系统分享集成等这些依靠系统服务的开发变成了可能.WWDC 2016上众多更 ...
- iOS开发系列--通讯录、蓝牙、内购、GameCenter、iCloud、Passbook系统服务开发汇总
--系统应用与系统服务 iOS开发过程中有时候难免会使用iOS内置的一些应用软件和服务,例如QQ通讯录.微信电话本会使用iOS的通讯录,一些第三方软件会在应用内发送短信等.今天将和大家一起学习如何使用 ...
- iOS开发系列--通知与消息机制
概述 在多数移动应用中任何时候都只能有一个应用程序处于活跃状态,如果其他应用此刻发生了一些用户感兴趣的那么通过通知机制就可以告诉用户此时发生的事情.iOS中通知机制又叫消息机制,其包括两类:一类是本地 ...
- iOS开发系列--地图与定位
概览 现在很多社交.电商.团购应用都引入了地图和定位功能,似乎地图功能不再是地图应用和导航应用所特有的.的确,有了地图和定位功能确实让我们的生活更加丰富多彩,极大的改变了我们的生活方式.例如你到了一个 ...
随机推荐
- HtmlPrefixScopeExtensions
http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
- Quartz.Net CronExpression表达式详解
Quartz.Net是我们常用的开源任务调度程序,其中最方便最强大的功能就是灵活多变的定时任务执行的支持.他靠什么来实现这个灵活的任务定时调度呢,就是咱们今天要详细分享的Cron Express表达式 ...
- js数据结构与算法存储结构
数据结构(程序设计=数据结构+算法) 数据结构就是关系,没错,就是数据元素相互之间存在的一种或多种特定关系的集合. 传统上,我们把数据结构分为逻辑结构和物理结构. 逻辑结构:是指数据对象中数据元素之间 ...
- MSP430F149学习之路——蓝牙模块
注意蓝牙模块的接法! #include <msp430x14x.h> ]; ; void int_clk() { BCSCTL1 &= ~XT2OFF; BCSCTL2 |= SE ...
- CLRS:build_max_heap(strorage in array)
//用满二叉树存储,从n/2处开始递归向上调整(n/2后均为叶子节点,无需调整)使得根最大 //满二叉树顺序存储,左子2i,右子2i+1: #include<stdio.h>#includ ...
- 使用扩展方法(this 扩展类型)
namespace ConsoleApp_UseExtendWays{ class Program { static void Main(string[] args) { Student s = ne ...
- gulp.spritesmith修改px为rem单位
移动端开发中,使用gulp.spritesmith进行小图sprite并生成样式,但由于spritesmith默认是以px为单位,所以就把插件的内容修改了下让生成rem单位并且能把background ...
- c++ builder TListView控件按字符串排序(根据网上代码亲测ok)
//--------------------------------------------------------------------------- /* 首先将一个列表框控件安放在Form上, ...
- 将windows系统装到USB存储设备
需求: 1)一般公司比较规范,计算机系统有严格的限制策略,如果自己不懂得如何更改或者没有权限更改,将极其不便. 2)计划在家里完成在公司未完成的事,甚至异地出差觉得携带笔记本不太方便,寻找更便携的设备 ...
- 2_2数据类型与C#部分语法[wp8特色开发与编程技巧]
2_2数据类型 -5min 类型介绍 在上个视频中我们构建了我们第一个应用.这一次我们要来了解下c#的数据类型 众所周知,在我们已认知的世界里,我们把文字分为数字与字符.在程序的世界里面我们把数据分为 ...