iPhone应用程序编程指南--应用程序状态管理.pdf

上传人:asd****56 文档编号:70334438 上传时间:2023-01-19 格式:PDF 页数:14 大小:504.76KB
返回 下载 相关 举报
iPhone应用程序编程指南--应用程序状态管理.pdf_第1页
第1页 / 共14页
iPhone应用程序编程指南--应用程序状态管理.pdf_第2页
第2页 / 共14页
点击查看更多>>
资源描述

《iPhone应用程序编程指南--应用程序状态管理.pdf》由会员分享,可在线阅读,更多相关《iPhone应用程序编程指南--应用程序状态管理.pdf(14页珍藏版)》请在得力文库 - 分享文档赚钱的网站上搜索。

1、我的共享资源:http:/ States and Multitasking-应用程序状态及多任务For iOS apps,it is crucial to know whether your app is running in the foreground or the background.Because resources are more limited on iOS devices,an app must behave differently in the background than in the foreground.The operating system also limit

2、s what your app can do in the background in order to improve battery life and to improve the users experience with the foreground app.The operating system notifies your app whenever it moves between the foreground and background.These notifications are your chance to modify your apps behavior.对于iOS应

3、用程序来说,了解程序当前正运行于后台或者前台是很重要的。因为iOS设备上的资源有限,应用程序处于后台时的行为必定要与处于前台时不同。为延长电池使用时间以及提升前台应用程序的用户体验,操作系统也限制了应用程序处于后台时可做的事情。在前台和后台切换的过程中,操作系统会通知你的应用程序,利用这些通知你可以适时地改变应用程序的行为。While your app is in the foreground,the system sends touch events to it for processing.The UIKit infrastructure does most of the hard wor

4、k of delivering events to your custom objects.All you have to do is override methods in the appropriate objects to process those events.For controls,UIKit simplifies things even further by handling the touch events for you and calling your custom code only when something interesting happens,such as

5、when the value of a text field changes.当应用程序处于前台时,系统会分发触摸事件给它处理。UIKit作为基础设施,承担了分派事件这一工作中最难的部分,而你所要做的是在合适的对象中重写一些方法用以处理这些事件。对于控件来说,UIKit更进一步,只有在一些特定的事件发生时才会去调用你自定义的处理代码,比如某个文本域的值发生变化,否则它会自行处理好触摸事件。As you implement your app,follow these guidelines:(Required)Respond appropriately to the state transitio

6、ns that occur.Not handling these transitions properly can lead to data loss and a bad user experience.For a summary of how to respond to state transitions,see“Managing App State Changes”.(Required)When moving to the background,make sure your app adjusts its behavior appropriately.For guidelines abou

7、t what to do when your app moves to the background,see“Being a Responsible Background App”.(Recommended)Register for any notifications that report system changes your app needs.When an app is suspended,the system queues key notifications and delivers them when the app resumes execution.Apps should u

8、se these notifications to make a smooth transition back to execution.For more information,see“Processing Queued Notifications at Wakeup Time”.(Optional)If your app needs to do actual work while in the background,ask the system for the appropriate permissions to continue running.For more information

9、about the types of background work you can do and how to request permission to do that work,see“Background Execution and Multitasking”.实现应用程序时,遵循以下原则:(必要)发生状态转换时适当地作出响应。转换中不恰当的处理可能导致数据损失和糟糕的用户体验。关于如何响应状态转换,参阅Managing App State Changes(必要)进入后台的时候,确保你的应用程序恰当地调整其行为。关于应用程序进入后台时应该执行的动作请参阅Being a Responsi

10、ble Background App(推荐)根据应用程序的需要注册报告系统变更的通知。当一个应用程序挂起时,系统会将关键的通知排队等候,并在应用程序恢复运行时发送这些通知给应用程序。应用程序应当利用这些通知平滑地转换回执行状态。更多信息请参阅Processing Queued Notifications at Wakeup Time(可选)如果应用程序需要在后台执行一些实际的工作,需要请求系统授予适当的权限以继续运行。关于可执行的后台任务类型以及如何请求执行权限,参阅“Background Execution and Multitasking.Managing App State Change

11、s-管理应用程序状态转换At any given moment,your app is in one of the states listed in Table 3-1.The system moves your app from state to state in response to actions happening throughout the system.For example,when the user presses the Home button,a phone call comes in,or any of several other interruptions occu

12、rs,the currently running apps change state in response.Figure 3-1 shows the paths that an app takes when moving from state to state.在任意给定时刻,你的应用程序处于表3-1中所列举的状态中的某个状态。系统响应诸如用户按下Home键,来电提醒或者任何其他中断事件时,将你的应用程序从一个状态转换为另一个状态。图3-1展示了一个应用程序在状态切换的路径。Table 3-1 3-1 App statesStateDescriptionNot runningThe app

13、has not been launched or was running but was terminated by the system.InactiveThe app is running in the foreground but is currently not receiving events.(It may be executing other code though.)An app usually stays in this state only briefly as it transitions to a different state.ActiveThe app is run

14、ning in the foreground and is receiving events.This is the normal mode for foreground app.BackgroundThe app is in the background and executing code.Most apps enter this state briefly on their way to being suspended.However,an app that requests extra execution time may remain in this state for a peri

15、od of time.In addition,an app being launched directly into the background enters this state instead of the inactive state.For information about how to execute code while in the background,see“Background Execution and Multitasking”(page 56).SuspendedThe app is in the background but is not executing c

16、ode.The system moves apps to this state automatically and does not notify them before doing so.While suspended,an app remains in memory but does not execute any code.When a low-memory condition occurs,the system may purge suspended apps without notice to make more space for the foreground app.表 3-1

17、3-1 应用程序状态状态描述未在运行应用程序未在运行或者已被系统终止。待激活应用程序运行在前台但当前还没有在接收事件。(可能正在执行其他代码)应用程序通常在进入另一状态前短暂地处于这种状态。激活应用程序运行于前台并且正在接收事件。这是前台应用程序的正常模式。后台应用程序正处于后台并且在执行指令。大部分应用程序在进入挂起状态前会短暂地经历这个状态。但是,如果一个应用程序请求额外的执行时间的话可能持续保持这种状态一段时间。另外,启动时直接进入后台的应用程序将不经过待激活状态而直接进入这个状态。参阅“Background Execution and Multitasking”.挂起应用程序处于后台并

18、且未在执行代码。系统会将处于这样的应用程序自动转换到这一状态并且并不会预先通知它们。处于挂起状态时,应用程序还驻留在内存中但并不执行任何指令。当内存不足的情况出现时,系统可能会将挂在的应用程序清理出去以便腾出空间给前台的应用程序,这个动作也不会通知被挂起的应用程序。NoteApps running in iOS3.2 and earlier do not enter the background or suspended states.In addition,some devices do not support multitasking or background execution at

19、all,even when running iOS 4 or later.Apps running on those devices also do not enter the background or suspended states.Instead,apps are terminated upon leaving the foreground.注意运行于iOS3.2或者更早版本系统的应用程序不会进入后台或者挂起状态。另外,有一些设备即便运行iOS 4或者更高版本系统也不支持多任务或者后台操作。运行于这些设备的应用程序也不会进入后台或者挂起状态,它们退出前台后就会被终止。Most stat

20、e transitions are accompanied by a corresponding call to the methods of your app delegate object.These methods are your chance to respond to state changes in an appropriate way.These methods are listed below,along with a summary of how you might use them.application:didFinishLaunchingWithOptions:Thi

21、s is your apps first chance to execute code at launch time.applicationDidBecomeActive:This is your apps chance to prepare to run as the foreground app.applicationWillResignActive:Lets you know that your app is transitioning away from being the foreground app.Use this method to put your app into a qu

22、iescent state.applicationDidEnterBackground:Lets you know that your app is now running in the background and may be suspended at any time.applicationWillEnterForeground:Lets you know that your app is moving out of the background and back into the foreground,but that it is not yet active.applicationW

23、illTerminate:Lets you know that your app is being terminated.This method is not called if your app is suspended.大部分状态转换是通过调用应用程序的delegate对象的一些方法实现的。这些方法是你响应各种状态切换的时机。下面列举了这些方法以及应该如何使用它们。application:didFinishLaunchingWithOptions:这里是应用程序启动时第一处可以执行代码的地方。applicationDidBecomeActive:这里为进入前台做准备。application

24、WillResignActive:应用程序即将退出前台状态。使用这个方法来使应用程序休眠。applicationDidEnterBackground:-应用程序已进入后台运行并且可能随时被挂起。applicationWillEnterForeground:-应用程序正在由后台进入前台的过程中,但尚未激活。applicationWillTerminate:应用程序正在被终止。如果应用程序已处于挂起状态,这个方法将不会被调用。The App Launch Cycle-应用程序启动周期When your app is launched,it moves from the not running st

25、ate to the active or background state,transitioning briefly through the inactive state.As part of the launch cycle,the system creates a process and main thread for your app and calls your apps main function on that main thread.The default main function that comes with your Xcode project promptly han

26、ds control over to the UIKit framework,which does most of the work in initializing your app and preparing it to run.Figure 3-2 shows the sequence of events that occurs when an app is launched into the foreground,including the app delegate methods that are called.当应用程序启动后,它将从未运行状态转入激活状态或者后台状态,转换期间短暂地

27、经历了“待激活”状态。作为启动周期的一部分,系统将创建一个进程并为你的应用程序建立一个运行main函数的主线程。XCode模版生成的默认的main函数会提醒你将控制权交给UIKit框架,这个框架为你完成了应用程序初始化和准备运行的大部分工作。图3-2显示了一个应用程序启动并进入前台的过程中的事件序列,包含应用程序delegate被调用的方法。If your app is launched into the background insteadusually to handle some type of background eventthe launch cycle changes sligh

28、tly to the one shown in Figure 3-3.The main difference is that instead of your app being made active,it enters the background state to handle the event and then is suspended shortly afterward.When launching into the background,the system still loads your apps user interface files but it does not dis

29、play the apps window.如果你的应用程序启动后是进入后台而非前台通常会处理一些后台类型的事件那么启动周期会有一点微小的改变,如图3-3所示。主要的区别是应用程序不进入激活状态而是进入后台状态并处理一些事件,随后很快被挂起。启动并进入后台的过程中,系统仍然会载入应用程序的用户接口文件但不会显示应用程序的窗口。To determine whether your app is launching into the foreground or background,check the applicationState property of the shared UIApplicat

30、ion object in your application:didFinishLaunchingWithOptions:method.When the app is launched into the foreground,this property contains the value UIApplicationStateInactive.When the app is launched into the background,the property contains the value UIApplicationStateBackground instead.You can use t

31、his difference to adjust the launch-time behavior of your application:didFinishLaunchingWithOptions:method accordingly.要确认你的应用程序是进入前台还是后台,可以在应用程序的didFinishLaunchingWithOptions方法中检查共享UIApplication对象的applicationState属性。当应用程序进入前台,这个属性的值是UIApplicationStateInactive;当应用程序进入后台,这个属性的值则是UIApplicationStateBac

32、kground。你可以根据这个值来调整你的应用程序在启动时的行为didFinishLaunchingWithOptions。Note When an app is launched so that it can open a URL,the sequence of startup events is slightly different from those shown in Figure 3-2 and Figure 3-3.For information about the startup sequences that occur when opening a URL,see“Handli

33、ng URL Requests”.注意当一个应用程序为打开一个URL而启动时,启动事件序列会与图3-2和图3-3有微小的区别。关于打开URL时产生的启动事件序列,参阅“Handling URL Requests”About the main function-关于主函数Like any C-based app,the main entry point for an iOS app at launch time is the main function.In an iOS app,the main function is used only minimally.Its main job is t

34、o hand control to the UIKit framework.Therefore,any new project you create in Xcode comes with a default main function like the one shown in Listing 3-1.With few exceptions,you should never change the implementation of this function.像任何C应用程序一样,iOS应用程序在启动时的主入口也是main函数。在iOS应用程序中,main函数被最简化地运用。它的主要工作是将

35、控制权移交给UIKit框架。因此,任何你在XCode中创建的新项目都会包含一个如列表3-1所示的默认的main函数。除了极个别情况,一般不对这个函数的实现进行修改。Listing 3-1 The main function of an iOS app#import int main(int argc,char*argv)autoreleasepool return UIApplicationMain(argc,argv,nil,NSStringFromClass(MyAppDelegate class);Note An autorelease pool is used in memory ma

36、nagement.It is a Cocoa mechanism used to defer the release of objects created during a functional block of code.For more information about autorelease pools,see Advanced Memory Management Programming Guide.注意autorelease pool用于内存管理,它是Cocoa中用以推迟释放功能性代码块中创建的对象的一种机制。关于更多autorelease pool的信息,参阅Advanced Me

37、mory Management Programming Guide.The UIApplicationMain function takes four parameters and uses them to initialize the app.You should never have to change the default values passed into this function.Still,it is valuable to understand their purpose and how they start the app.The argc and argv parame

38、ters contain any launch-time arguments passed to the app from the system.These arguments are parsed by the UIKit infrastructure and can otherwise be ignored.The third parameter identifies the name of the principal app class.This is the class responsible for running the app.It is recommend that you s

39、pecify nil for this parameter,which causes UIKit to use the UIApplication class.The fourth parameter identifies the class of your custom app delegate.Your app delegate is responsible for managing the high-level interactions between the system and your code.The Xcode template projects set this parame

40、ter to an appropriate value automatically.UIApplicationMain函数接收四个参数并用它们来初始化应用程序。虽然你不需要修改传递给这个函数的默认参数,但是了解这些参数的用途以及应用程序如何启动还是有意义的。参数argc和argv包含了启动期由系统传递给应用程序的参数。这些参数由UIKit解析或者被忽略。第三个参数标识最主要的应用程序类。这个类负责运行应用程序,建议将这个参数指定为nil,这个情况下UIKit会使用UIApplication类来运行应用程序。第四个参数标识应用程序的delegate类。应用程序的delegate负责管理你的代码和

41、系统之间高层次的交互。XCode模版项目会自动将这个参数设置了合适的值。An app can have either a main storyboard file or a main nib file but cannot have both.Storyboards,the preferred way to specify your apps user interface,are not supported on all versions of iOS.Specify the name of your main storyboard file in the UIMainStoryboardFi

42、le key of your apps Info.plist file.(For nib-based apps,specify the name of your main nib file using the NSMainNibFile key instead.)Normally,Xcode sets the value of this key when you create your project,but you can change it later if needed.For more information about the Info.plist file and how you

43、use it to configure your app,see Figure 6-1.应用程序可以使用一个主storyboard文件或者一个主nib文件但不能同时使用两者。Storyboard是指定应用程序用户接口的优先方案,但并非所有的iOS版本都支持。主storyboard文件的名字是由应用程序的Info.plist文件中的UIMainStoryboardFile键指定。通常,XCode在你创建项目时就会设定该键的值,有需要的话可以修改。关于Info.plist以及如何配置应用程序的更多信息,参阅图6-1.What to Do at Launch Time-启动期应该做些什么When y

44、our app is launched(either into the foreground or background),use your app delegates application:didFinishLaunchingWithOptions:method to do the following:Check the contents of the launch options dictionary for information about why the app was launched,and respond appropriately.Initialize the apps c

45、ritical data structures.Prepare your apps window and views for display.Apps that use OpenGL ES should not use this method to prepare their drawing environment.Instead,they should defer any OpenGL ES drawing calls to the applicationDidBecomeActive:method.Use any saved preferences or state information

46、 to restore the app to its previous runtime state.应用程序启动后(不论进入前台还后台),使用应用程序的delegate的application:didFinishLaunchingWithOptions方法来执行以下动作:检查启动选项字典的内容来获悉应用程序启动的目的并适当地作出响应。初始化应用程序的关键数据结构准备应用程序的窗口和视图以备显示。使用OpenGL ES的应用程序不能用这个方法来准备绘图环境,而应该推迟到applicationDidBecomeActive中调用Open ES绘图操作使用任何保存的属性或者状态信息来还原应用程序到它

47、之前的运行状态If your app uses nib files to manage its views,you should use the application:didFinishLaunchingWithOptions:method to prepare your apps window for display.For nib-based apps,you must create your window object,install the views from your initial view controller,and show the window.For apps tha

48、t support both portrait and landscape orientations,always set up your window in a portrait orientation.If the device is in a different orientation at launch time,the system automatically rotates your views to the appropriate orientation before displaying the window.如果你的应用程序使用nib文件来管理视图,那么你应该使用applic

49、ation:didFinishLaunchingWithOptions:方法来准备用于显示的应用程序窗体。针对基于nib的应用程序,你应该创建窗体对象,通过初始视图控制器中安装视图,然后显示窗体。对于同时支持垂直和水平方向的应用程序,要将窗体设置为垂直方向。如果设备在应用程序启动时处于水平方向,系统会在显示窗体之前自动旋转视图到合适的方向。Your application:didFinishLaunchingWithOptions:method should always be as lightweight as possible to reduce your apps launch time

50、.Apps are expected to launch and initialize themselves and start handling events in roughly 5 seconds.If an app does not finish its launch cycle in a timely manner,the system kills it for being unresponsive.Thus,any tasks that might slow down your launch(such as accessing the network)should be execu

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 技术资料 > 其他杂项

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知得利文库网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号-8 |  经营许可证:黑B2-20190332号 |   黑公网安备:91230400333293403D

© 2020-2023 www.deliwenku.com 得利文库. All Rights Reserved 黑龙江转换宝科技有限公司 

黑龙江省互联网违法和不良信息举报
举报电话:0468-3380021 邮箱:hgswwxb@163.com