如何知道当前设备是否可以有后台模式

发布于 2024-11-04 14:44:16 字数 349 浏览 0 评论 0原文

进入后台模式后我正在执行一些任务。我只想在后台模式下执行这些任务,所以我这样做了:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

萌︼了一个春 2024-11-11 14:44:16

您可以使用-respondsToSelector检查当前iOS是否支持返回当前应用程序状态:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(applicationState)] ){
  UIApplicationState state = [[UIApplication sharedApplication] applicationState];
}

您可以确定是否支持多任务(即使运行iOS 4或更高版本的设备也可能不具备支持多任务的硬件)通过专门检查后台支持:

UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
   backgroundSupported = device.multitaskingSupported;

您可以使用以下代码检索iOS版本(尽管您不应该真正依赖以下代码来推断后台支持):

float osVersion=[[[UIDevice currentDevice] systemVersion] floatValue];

最后,如果您想在进入后台模式后立即执行某些任务,你会想要利用以下活动:

- (void)applicationDidEnterBackground:(UIApplication *)application

..但请注意,您的时间将非常有限。您将有大约五秒钟的时间来做您想做的事情。如果您需要更多时间,可以尝试向 iOS 请求额外时间:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication*    app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task.

        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

请注意,在这种情况下,iOS 可能会(或不会)授予您更多时间,但这仍然是有限长度的时间。

苹果的 在后台执行代码值得一读。

You can check whether the current iOS supports returning the current application state by using -respondsToSelector:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(applicationState)] ){
  UIApplicationState state = [[UIApplication sharedApplication] applicationState];
}

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:

UIDevice* device = [UIDevice currentDevice];
BOOL backgroundSupported = NO;
if ([device respondsToSelector:@selector(isMultitaskingSupported)])
   backgroundSupported = device.multitaskingSupported;

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):

float osVersion=[[[UIDevice currentDevice] systemVersion] floatValue];

Finally, if you want to perform some tasks just after having entered background mode, you'll want to take advantage of the following event:

- (void)applicationDidEnterBackground:(UIApplication *)application

.. 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:

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    UIApplication*    app = [UIApplication sharedApplication];

    bgTask = [app beginBackgroundTaskWithExpirationHandler:^{
        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    }];

    // Start the long-running task and return immediately.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        // Do the work associated with the task.

        [app endBackgroundTask:bgTask];
        bgTask = UIBackgroundTaskInvalid;
    });
}

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.

栀子花开つ 2024-11-11 14:44:16

我发现此链接对于了解如何处理 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文