OGeek|极客世界-中国程序员成长平台

标题: ios - 在 iOS 7 上 backgroundTimeRemaining 少于 10 秒 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:39
标题: ios - 在 iOS 7 上 backgroundTimeRemaining 少于 10 秒

我会定期获得约 9 秒的后台剩余时间。我相信它应该接近180秒。然而,这不是我所看到的。

- (void)applicationDidEnterBackgroundUIApplication *)application
{
    UIApplication *app = [UIApplication sharedApplication];
    double secondsToStayOpen = app.backgroundTimeRemaining;
   NSLog(@"secondsToStayOpen %f ",secondsToStayOpen);
 }

打印

secondsToStayOpen 179.920931 
secondsToStayOpen 9.959715 
secondsToStayOpen 9.962670 



Best Answer-推荐答案


对于 180 年代...您必须“创建”一个 iOS 后台任务。 请修改您的代码:

if([[UIDevice currentDevice] respondsToSelectorselector(isMultitaskingSupported)])
{
    NSLog(@"Multitasking Supported");

    __block UIBackgroundTaskIdentifier background_task;
    background_task = [application beginBackgroundTaskWithExpirationHandler:^ {

        //Clean up code. Tell the system that we are done.
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    }];

    //To make the code block asynchronous
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        //### background task starts
        NSLog(@"Running in the background\n");
        while(TRUE)
        {
            NSLog(@"Background time Remaining: %f",[[UIApplication sharedApplication] backgroundTimeRemaining]);
            [NSThread sleepForTimeInterval:1]; //wait for 1 sec
        }
        //#### background task ends

        //Clean up code. Tell the system that we are done.
        [application endBackgroundTask: background_task];
        background_task = UIBackgroundTaskInvalid;
    });
}
else
{
    NSLog(@"Multitasking Not Supported");
}

从这里:http://hayageek.com/ios-background-task/

关于ios - 在 iOS 7 上 backgroundTimeRemaining 少于 10 秒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24542724/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4