【问题标题】:iOS - Is scheduling background task with NSTimer still restricted by Apple?iOS - 使用 NSTimer 调度后台任务是否仍受 Apple 限制?
【发布时间】:2013-03-09 05:46:47
【问题描述】:

我知道我在拉稻草,但希望得到答案。

我有一个应用需要在后台与服务器通信,无需用户干预。在推送通知到达时或至少定期(每 5 分钟左右)。

我可能会附加位置更新并将其发送到服务器。我什至可以证明使用粗略的位置是合理的,因此 Apple 会批准它。在最坏的情况下,我将支付 299 美元购买企业开发者许可证并使用本地分发(我唯一关心的)。

我最后的希望是使用 NSTimer 定期调度后台任务。 使用此方法的应用程序是否仍受 Apple 对允许的后台任务类型的限制? (媒体/位置/voip/newsstand/external device)这看起来很歧视......

附言。该应用程序现在在 ipod touch 上以开发模式运行,包括与服务器的定期通信。就后台模式而言,我没有在 plist 中声明任何内容。这是否意味着我没事?

【问题讨论】:

    标签: ios background nstimer


    【解决方案1】:

    我已经实施了以下内容,并于昨天获得了苹果的批准。我的应用不支持(媒体/位置/voip/报亭/外部设备)

    通过在 applicationDidEnterBackground 中使用以下代码,您可以获得 600 秒(10 分钟)的最大时间:

    if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)]) { //Check if our iOS version supports multitasking I.E iOS 4
    if ([[UIDevice currentDevice] isMultitaskingSupported]) { //Check if device supports mulitasking
    UIApplication *application = [UIApplication sharedApplication]; //Get the shared application instance
    __block UIBackgroundTaskIdentifier background_task; //Create a task object
    background_task = [application beginBackgroundTaskWithExpirationHandler: ^ {
        [application endBackgroundTask: background_task]; //Tell the system that we are done with the tasks
        background_task = UIBackgroundTaskInvalid; //Set the task to be invalid
        //System will be shutting down the app at any point in time now
    }];
    //Background tasks require you to use asyncrous tasks
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        //Perform your tasks that your application requires
        NSLog(@"\n\nRunning in the background!\n\n");
        [application endBackgroundTask: background_task]; //End the task so the system knows that you are done with what you need to perform
        background_task = UIBackgroundTaskInvalid; //Invalidate the background_task
    });
    }
    
    }
    

    文档可以在这里找到http://disanji.net/iOS_Doc/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/BackgroundExecution/BackgroundExecution.html

    我刚刚实现了 backgroundTaskIdentifier 对象并使 background_task 无效以检查时间,应用程序处于活动状态并且正在运行 600 秒。你甚至可以通过这个来获取剩余时间

    NSLog(@"Time remaining: %f", application.backgroundTimeRemaining);
    

    【讨论】:

    • 我想我在网上看到过类似的代码。有人尝试在接近到期时间时分派另一个 bg 任务的技巧......不确定这是否会导致应用被踢出。
    • 如果任务没有在 10 分钟(即 600 秒)内结束,那么应用程序将自动终止。所以我在 applicationWillTerminate 中编写了代码来处理恢复选项,如果下载正在进行,否则如果下载的很少,我只是删除它并将其设置回不可用。
    • 只添加此代码将使您的应用程序保持活力,我在单独的线程上进行下载,并没有在此代码中添加任何内容。
    • 请原谅可能是愚蠢的问题,但在执行 NSLog(@"\n\nRunning in the background!\n\n"); - 而不是等待 10 分钟并调用 expirationHandler?
    • 我没有添加任何后台任务,我已经删除了这段代码 //后台任务要求你使用异步任务 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //执行你的任务application requires NSLog(@"\n\nRunning in the background!\n\n"); [application endBackgroundTask: background_task]; //结束任务,让系统知道你已经完成了你需要执行的任务 background_task = UIBackgroundTaskInvalid ; //使background_task失效 });
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-01
    相关资源
    最近更新 更多