程序笔记   发布时间:2022-05-30  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了App Programming Guide for iOS --Background Execution 2大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

BACkground Execution 2

Being a Responsible BACkground App 成为一个负责的后台执行的app

The foreground app always has precedence over BACkground apps when it comes to the use of system resources and harDWare. Apps running in the BACkground need to be prepared for this discrepancy and adjust their behavior when running in the BACkground. Specifically,apps moving to the BACkground should follow these guIDelines:
当涉及到系统资源和硬件的使用时,在前台的应用总是优先于后台应用程序。在后台运行的应用程序需要为这种差异做好准备,并在后台运行时调整它们的行为。具体来说,移动到后台的应用程序应该遵循以下指导原则:

  • Do not make any OpenGL ES calls from your code. You must not create an EAGLContext object or issue any OpenGL ES drawing commands of any kind while running in the BACkground. Using these calls causes your app to be killed immediately. Apps must also ensure that any prevIoUsly submitted commands have completed before moving to the BACkground. For information about how to handle OpenGL ES when moving to and from the BACkground,see ImplemenTing a Multitasking-aware OpenGL ES Application in OpenGL ES ProgrAMMing GuIDe.
    不要从代码中调用OpenGL ES。您不能在后台运行时创建EAGLContext对象或执行任何OpenGL ES绘图命令。使用这些调用会使您的应用程序立即被杀死。应用程序还必须确保在进入后台之前任何之前提交的命令都已经完成。有关在前后台切换时如何处理OpenGL ES的信息,请参阅在OpenGL ES编程指南中的实现 Multitasking-aware OpenGL ES应用程序。
  • Cancel any Bonjour-related services before being suspended. When your app moves to the BACkground,and before it is suspended,it should unregister from Bonjour and close Listening sockets associated with any network services. A suspended app cAnnot respond to incoming service requests anyway. Closing out those services prevents them from appearing to be available when they actually are not. If you do not close out Bonjour services yourself,the system closes out those services automatically when your app is suspended.
    当您的应用程序移到后台时,在挂起之前,取消任何与Bonjour相关的服务。它应该从Bonjour中取消注册,并关闭与任何网络服务相关的侦听套接字。挂起的应用程序无论如何都不能响应传入的服务请求。关闭这些服务会防止它们在实际不能使用时出现可用的情况发生。如果您不亲自关闭Bonjour服务,则当应用程序暂停时,系统会自动关闭这些服务。
  • Be prepared to handle connection @R_516_4895@ in your network-based sockets. The system may tear down socket connections while your app is suspended for any number of reasons. As long as your socket-based code is prepared for other types of network @R_516_4895@,such as a lost signal or network Transition,this should not lead to any unusual problems. When your app resumes,if it encounters a failure upon using a socket,simply reestablish the connection.
    准备好处理基于网络的套接字连接失败的故障。当应用程序因为各种原因被挂起时系统可能会拆除套接字连接。只要你的基于套接字的代码为其他类型的网络故障做好准备,例如丢失信号或网络过渡(4G->3G),这就不会导致任何不寻常的问题。当应用程序恢复时,如果它在使用套接字时遇到故障,只需重新建立连接即可。
  • Save your app state before moving to the BACkground. During low-memory conditions,BACkground apps may be purged from memory to free up space. Suspended apps are purged first,and no notice is given to the app before it is purged. As a result,apps should take advantage of the state preservation mechanism in iOS 6 and later to save their interface state to disk. For information about how to support this feature,see Preserving Your App’s Visual Appearance Across Launches.
    在移到后台之前保存您的应用程序状态。在内存不足的情况下,后台应用程序可能会从内存中清除,以腾出空间。被挂起的应用程序首先被清除,在清除应用程序之前,App不会得到通知。因此,应用程序应该利用iOS 6和更高版本中的状态保存机制将它们的interface(界面以及变量)状态保存到磁盘上。有关如何支持此功能的信息,请参阅保留应用程序的可视化外观。
  • Remove strong references to unneeded objects when moving to the BACkground. If your app maintains a large in-memory cache of objects (especially images),remove all strong references to those caches when moving to the BACkground. For more information,see Reduce Your Memory Footprint.
    移到后台时,删除对不需要的对象的强引用。如果您的应用程序维护了大量的对象内存缓存(特别是图像),那么在移动到后台时,删除对这些缓存的所有强引用。有关更多信息,请参见减少内存占用。
  • Stop using shared system resources before being suspended. Apps that interact with shared system resources such as the Address Book or calendar databases should stop using those resources before being suspended. Priority for such resources always goes to the foreground app. When your app is suspended,if it is found to be using a shared resource,the app is killed.
    在挂起之前停止使用共享系统资源。与共享系统资源(如通讯簿或日历数据库)交互的应用程序应该在挂起之前停止使用这些资源。对这些资源的优先级总是放在前台应用程序上。当您的应用程序被挂起时,如果发现它正在使用共享资源,则该应用程序将被杀死。
  • AvoID updating your windows and vIEws. Because your app’s windows and vIEws are not visible when your app is in the BACkground,you should avoID updating them. The exception is in cases where you need to update the contents of a window prior to having a snapshot of your app taken.
    避免更新windows和vIEws。因为当应用程序处于后台时,应用程序的windows和vIEws是不可见的,所以您应该避免更新它们。例外情况是,您需要更新窗口的内容,然后才能获得应用程序的快照(例外是创建新的快照时必须先更新界面内容)。
  • Respond to connect and disconnect notifications for external accessorIEs. For apps that communicate with external accessorIEs,the system automatically sends a disconnection notification when the app moves to the BACkground. The app must register for this notification and use it to close out the current accessory session. When the app moves BACk to the foreground,a matching connection notification is sent,giving the app a chance to reconnect. For more information on handling accessory connection and disconnection notifications,see External Accessory ProgrAMMing topics.
    响应与外部配件连接和断开时的通知。对于与外部配件通信的应用程序,当应用程序移动到后台时,系统会自动发送断开连接通知。应用程序必须注册此通知,并使用它关闭当前与配件会话。当应用程序移回前台时,将发送一个与之相匹配的连接通知,给应用程序重新连接的机会。有关处理配件连接和断开连接通知的详细信息,请参阅外部配件编程主题。
  • Clean up resources for active alerts when moving to the BACkground. In order to preserve context when switching between apps,the system does not automatically dismiss action sheets (UIActionSheet) or alert vIEws (UIAlertVIEw) when your app moves to the BACkground. it is up to you to provIDe the appropriate cleanup behavior prior to moving to the BACkground. For example,you might want to cancel the action sheet or alert vIEw progrAMMatically or save enough contextual information to restore the vIEw later (in cases where your app is terminated).
    移动到后台时,清除活的alerts资源。为了在应用程序之间切换时保留上下文,当应用程序移动到后台时,系统不会自动关闭操作表(UIActionSheet)或alter视图(UIAlertVIEw)。在移到后台之前,应该由您提供适当的清理行为。例如,您可能希望以编程方式取消action sheet或alert vIEw,或者保存足够的上下文信息,以便稍后恢复视图(在应用程序终止的情况下)。
  • Remove sensitive information from vIEws before moving to the BACkground. When an app Transitions to the BACkground,the system takes a snapshot of the app’s main window,which it then presents brIEfly when Transitioning your app BACk to the foreground. Before returning from your applicationDIDEnterBACkground: method,you should hIDe or obscure passwords and other sensitive personal information that might be captured as part of the snapshot.
    移到后台前,从视图中删除敏感信息。当应用程序转换到后台时,系统会对应用程序的主窗口截取快照,然后在将应用程序转换回前台时简要地显示该快照。在 applicationDIDEnterBACkground: 方法返回之前,应该隐藏或掩盖可能作为快照一部分被捕获的密码和其他敏感个人信息。
  • Do minimal work while running in the BACkground. The execution time given to BACkground apps is more consTrained than the amount of time given to the foreground app. Apps that spend too much time execuTing in the BACkground can be throttled BACk by the system or terminated.
    在后台运行时,要做最少的工作。给后台应用程序的执行时间比给前台应用程序的时间更受限制。在后台执行时间过长的应用程序可能会被系统节流或终止。

If you are implemenTing a BACkground audio app,or any other type of app that is allowed to run in the BACkground,your app responds to incoming messages in the usual way. In other words,the system may notify your app of low-memory warnings when they occur. And in situations where the system needs to terminate apps to free even more memory,the app calls its delegate’s applicationWillTerminate: method to perform any final tasks before exiTing.
如果您正在实现在后台播放音频应用程序,或允许在后台运行的任何其他类型的应用程序,则您的应用程序将以通常的方式响应传入的消息。换句话说,系统可能会在出现低内存警告时通知应用程序。在系统需要终止应用程序以释放更多内存的情况下,在退出之前调用 app delegate 的applicationWillTerminate: 方法以执行任何最后工作。

OpTing Out of BACkground Execution 选择退出后台执行

If you do not want your app to run in the BACkground at all,you can explicitly opt out of BACkground by adding the UIApplicationExitsOnSuspend key (with the value YES) to your app’s Info.pList file. When an app opts out,it cycles between the not-running,inactive,and active states and never enters the BACkground or suspended states. When the user presses the Home button to quit the app,the applicationWillTerminate: method of the app delegate is called and the app has approximately 5 seconds to clean up and exit before it is terminated and moved BACk to the not-running state.
如果您根本不希望应用程序在后台运行,可以通过将uIApplicationExitsOnSuspend键(值为YES)添加到应用程序的Info.pList文件中,从而显式地选择退出后台。当应用程序选择退出时,它会在不运行、不活动和活动状态之间循环,并且永远不会进入后台或挂起状态。当用户按Home键退出应用程序时,会调用app delegate applicationWillTerminate:方法,并且应用程序在终止并移回未运行状态之前有大约5秒的时间清理和退出

OpTing out of BACkground execution is strongly discouraged but may be the preferred option under certain conditions. Specifically,if Coding for BACkground execution adds significant complexity to your app,terminaTing the app might be a simpler solution. Also,if your app consumes a large amount of memory and cAnnot easily release any of it,the system might kill your app quickly anyway to make room for other apps. Thus,opTing to terminate,instead of switching to the BACkground,might yIEld the same results and save you development time and effort.
选择退出后台执行是非常不鼓励的,但在某些条件下可能是首选方案。具体来说,如果为后台执行编写代码会给应用程序增加很大的复杂性,那么终止应用程序可能是一个更简单的解决方案。另外,如果你的应用程序消耗了大量的内存,并且不能轻易地释放其中的任何一个,系统可能很快就会杀死你的应用程序,以便为其他应用程序腾出空间。因此,选择终止,而不是切换到后台,可能会产生相同的结果,并节省您的开发时间和精力。

For more information about the keys you can include in your app’s Info.pList file,see information Property List Key Reference.
有关您可以在应用程序的Info.pList文件中包含的键的更多信息,请参见信息属性列表键引用。

大佬总结

以上是大佬教程为你收集整理的App Programming Guide for iOS --Background Execution 2全部内容,希望文章能够帮你解决App Programming Guide for iOS --Background Execution 2所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:for