如何知道当前设备是否可以有后台模式
进入后台模式后我正在执行一些任务。我只想在后台模式下执行这些任务,所以我这样做了:
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
但是 applicationState 仅在 iOS 4.0 及更高版本中可用...
我想知道这段代码如何在 iOS 3 上工作。
- 我如何知道设备是否有 iOS 4?
- 如何避免 iOS < 的崩溃和异常4 ?
感谢您的帮助
Kheraud
I am doing some tasks after having entered in background mode. I want to do these tasks only if I am in background mode so I did :
if ([[UIApplication sharedApplication] applicationState] == UIApplicationStateBackground)
But applicationState is only available in iOS 4.0 and later...
I wonder how this piece of code will work on iOS 3 for example.
- How can I know if the device has iOS 4 ?
- How can I avoid crash and exceptions for iOS < 4 ?
Thansk for your help
Kheraud
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
-respondsToSelector检查当前iOS是否支持返回当前应用程序状态:
您可以确定是否支持多任务(即使运行iOS 4或更高版本的设备也可能不具备支持多任务的硬件)通过专门检查后台支持:
您可以使用以下代码检索iOS版本(尽管您不应该真正依赖以下代码来推断后台支持):
最后,如果您想在进入后台模式后立即执行某些任务,你会想要利用以下活动:
..但请注意,您的时间将非常有限。您将有大约五秒钟的时间来做您想做的事情。如果您需要更多时间,可以尝试向 iOS 请求额外时间:
请注意,在这种情况下,iOS 可能会(或不会)授予您更多时间,但这仍然是有限长度的时间。
苹果的 在后台执行代码值得一读。
You can check whether the current iOS supports returning the current application state by using
-respondsToSelector:
You can determine whether multitasking support is available (even devices running iOS 4 or later may not have the hardware to support multitasking) by specifically checking for background support:
You can retrieve the iOS version by using the following code (although you should not really rely on the following code to infer background support):
Finally, if you want to perform some tasks just after having entered background mode, you'll want to take advantage of the following event:
.. but note that your time will be very limited. You will have approximately five seconds to do whatever you want to do. In case you need more, you can try asking iOS for additional time:
Note that in this case, you might be granted more time by iOS (or not), but it's still a finite-length time.
Apple's Executing Code in the Background is a worth read.
我发现此链接对于了解如何处理 3.1 和 >4.0 操作系统版本非常有用:
使用 iOS4 SDK 开发 iPhone 应用程序,部署到 3.x 设备:基础 SDK 和 iPhone OS 部署目标
I discovered this link very usefull to understand how to handle both 3.1 and >4.0 OS versions :
Developing iPhone Apps with iOS4 SDK, Deploying to 3.x Devices : Base SDK and iPhone OS Deployment Target