详解IOS多任务机制 初学者必看文档

移动开发 iOS
本文主要介绍iOS多任务机制的内容,对于大多数的应用,如果不是特别需要,用最新的SDK编译出来的程序本身就是支持多任务的—按home键程序进入后台运行,详细内容来看内容。

IOS多任务机制是本文要介绍的内容,对IOS程序持久化很感兴趣,于是首先研究了下iOS多任务机制.对于大多数的应用,如果不是特别需要,用***的SDK编译出来的程序本身就是支持多任务的—按home键程序进入后台运行(但是注意此时的程序并不是会运行,只是进入后台状态便于其再次进入活动状态,这一点同我们概念中应该有的多任务有区别).而对于一些应用,是需充分运用iOS多任务的特性,如游戏应用和需要网络连接的应用等等。

多任务机制是苹果在iOS 4中引进的,我们首先新建一个工程,查看下appdelegate文件中的内容:

  1. App cycle   
  2. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions   
  3. {            /  
  4. / Override point for customization after application launch.      
  5. // Set the view controller as the window's root view controller and display.    return YES;  
  6. }  
  7. - (void)applicationWillResignActive:(UIApplication *)application   
  8. {      
  9. /*     Sent when the application is about to move from active to inactive state.   
  10. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or
  11.  when the user quits the application and it begins the transition to the background state.       
  12. Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. 
  13. Games should use this method to pause the game.       
  14. */  
  15. }  
  16. - (void)applicationDidEnterBackground:(UIApplication *)application   
  17. {      
  18. /* Use this method to release shared resources, save user data, invalidate timers,  
  19.  and store enough application state information to restore your application to its current state in case it is terminated later.        
  20. If your application supports background execution, called instead of applicationWillTerminate: when the user quits.       
  21. */  
  22. }  
  23. - (void)applicationWillEnterForeground:(UIApplication *)application   
  24. {      
  25. /*       
  26. Called as part of  transition from the background to the inactive state: here you can undo many of the changes made on entering the background.
  27. */  
  28. }  
  29. - (void)applicationDidBecomeActive:(UIApplication *)application   
  30. {      
  31. /*     Restart any tasks that were paused (or not yet started) while the application was inactive.   
  32. If the application was previously in the background, optionally refresh the user interface.       
  33. */  
  34. }  
  35. - (void)applicationWillTerminate:(UIApplication *)application   
  36. {      
  37. /*     Called when the application is about to terminate.        
  38. See also applicationDidEnterBackground:.      
  39.  */  

以上的函数就是ios4中引入的用于支持多任务运行的函数.从函数名我们就可以猜测到程序可能的一些状态:background,active. 图1就是程序的生命周期图.

详解IOS多任务机制 初学者必看文档

图1

我们来看一下:程序***次启动,从Not Running状态进入active阶段,程序会调用两个函数:

  1. didFinishLaunchingWithOptions:和   
  2. applicationDidBecomeActive:  

此时点击home键,程序就会进入后台直至进入挂起状态,程序会依次调用下述函数:

  1. applicationWillResignActive:   
  2. applicationDidEnterBackground:  

这时双击home键再进入该程序时,程序会依次调用:

  1. applicationWillEnterForeground:   
  2. applicationDidBecomeActive:  

注意到Background包含running和suspend两种状态.这里的running不是真正意义上的程序运行,而是指的applicationDidEnterBackground:运行部分,通常情况下,系统给此函数执行的时间不会太多,并且执行完后程序就在后台挂起.这就是iOS多任务的绝大部分情形,但是等一下,就三种情况苹果是允许你在后台运行的                                      

音乐

位置

VoIP

你可以在程序中的plist文件中进行设置,如图2:

详解IOS多任务机制 初学者必看文档

图2

可以这么说,除了这三种服务允许以我们通常认为的多任务机制在后台运行,其他的程序的多任务,就是上文所指的程序状态.这里不讨论为啥苹果要这样做,只是稍微说明一下,在这种情况下,至少是在表面上实现了多任务:比如说我玩了愤怒的小鸟又想看下天气,然后又想玩小鸟了,这时切换就比较容易,因为程序这时是在后台挂起,还是在内存中运行着的,这样再启动时就会比较快.

程序调用相应函数的时候,系统会发送相应的Notification,这时app就应该适时的保存app状态或是读取app的历史状态,这样才能更好的呈现用户体验,之前说过,程序进入后台的时间很短,有时候app要做的操作还没来得及进行.这时,我们可以使用beginBackgroundTaskWithExpirationHandler:来处理耗时可能比较长的操作.

iOS所谓的多任务并不是我们通常以为的多任务,IOS只允许三种服务在后台运行;其他的只是方便多个app之间的切换.,至于如何实现多任务,比如进入后台程序应该优先进行哪些操作的细节,会在以后结合本人的实际加以说明。

小结:详解iOS多任务机制 初学者必看文档的内容介绍完了,希望本文读你有所帮助!

责任编辑:zhaolei 来源: 博客园
相关推荐

2011-08-04 18:01:07

IOS Cocoa Touc

2011-09-08 10:38:37

Widget

2011-09-16 09:38:19

Emacs

2011-08-10 16:44:56

iPhone代理设计模式

2011-09-14 09:49:41

Android开发

2011-11-08 17:11:47

程序员

2011-07-27 10:02:12

iPhone Framework

2011-06-17 14:29:55

Qt

2011-08-24 17:05:01

Lua

2011-06-17 15:32:28

Qt

2011-06-17 15:06:14

Qt

2011-06-17 14:12:32

Qt

2011-06-17 14:54:31

Qt

2011-06-17 15:44:25

Qt

2011-06-17 15:25:18

Qt

2011-06-17 15:19:28

Qt

2011-06-17 15:37:42

Qt

2013-04-23 10:51:15

Linux压缩

2011-06-17 14:41:56

Qt

2011-07-26 17:55:16

iPhone Runtime
点赞
收藏

51CTO技术栈公众号