Swift - 在iphone应用程序的后台运行计时器的最佳方式

时间:2016-01-24 22:38:14

标签: ios swift

我对iOS开发完全不熟悉,正在开发一款iPhone烹饪应用程序,可让用户选择三种“计时器”选项。第一个计时器运行6分钟,第二个计时器运行8.5分钟,最后一个计时器运行11分钟。

计时器完成倒计时后,它会播放音频文件并在应用程序屏幕中显示一条消息。一切都很完美,除了我在测试中发现计时器在用户转到另一个应用程序时停止运行(例如检查电子邮件,使用Safari等)。显然,这会破坏应用程序的目的,因为用户需要知道计时器何时完成,以便他们可以进行下一步(例如从炉子中取出一个平底锅)。

我研究了背景模式,我感到很困惑。似乎我没有理由(根据Apple)在后台运行这个应用程序(即它不播放音乐,使用位置服务等)。我还继续读到,否则在后台运行会有10分钟的限制。

我也遇到了本地和远程通知的想法,但Apple的开发者网站上不再存在我被引用的页面。我现在感到很茫然,很困惑。

有没有办法让我真正让这个应用程序在后台工作长达11分钟?如果是这样,怎么样?

这是一个更新。我一直试图了解本地通知和后台任务。

当地通知 这表明了一些承诺,但我不确定如何在我的场景中实现这一点?在通知出现/播放声音之前,我如何确保经过适当的时间?

例如,用户在正好12:00:00 pm选择“煮熟的鸡蛋”按钮,应用程序启动计数器6分钟。在下午12:01:20,用户阅读电子邮件,在12:01:50将手机放下之前需要30秒才能阅读报纸。让我们假设在12:02:50电话进入锁定模式,我如何确保本地通知触发3分钟和10秒以后组成整个6分钟并播放声音文件通知用户他们的蛋准备好了。

背景任务 如果我可以启动并重新启动后台任务以允许我的计时器在播放声音之前完成,这可能适用于我的场景。

下面是我的代码片段(与上面的鸡蛋示例相关),我希望这有助于将我的应用放在上下文中:

@IBAction internal func ButtonSoft(sender: UIButton) {

    counter = 360
    TimerDisplay.text = String("06:00")

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounter"), userInfo: "Eggs done!!", repeats: true)


    ButtonSoft.alpha = 0.5
    ButtonMedium.alpha = 0.5
    ButtonHard.alpha = 0.5
    ButtonSoft.enabled = false
    ButtonMedium.enabled = false
    ButtonHard.enabled = false


}




@IBAction internal func ButtonMedium(sender: UIButton) {

    counter = 510
    TimerDisplay.text = String("08:30")

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounter"), userInfo: "Eggs done!!", repeats: true)


    ButtonSoft.alpha = 0.5
    ButtonMedium.alpha = 0.5
    ButtonHard.alpha = 0.5
    ButtonSoft.enabled = false
    ButtonMedium.enabled = false
    ButtonHard.enabled = false


}

@IBAction internal func ButtonHard(sender: UIButton) {

    counter = 660
    TimerDisplay.text = String("11:00")

    timer = NSTimer.scheduledTimerWithTimeInterval(1, target: self, selector: Selector("updateCounter"), userInfo: "Eggs done!!", repeats: true)


    ButtonSoft.alpha = 0.5
    ButtonMedium.alpha = 0.5
    ButtonHard.alpha = 0.5
    ButtonSoft.enabled = false
    ButtonMedium.enabled = false
    ButtonHard.enabled = false


}


func stopTimer() {

    if counter == 0 {
        timer.invalidate()

    }
}


func updateCounter() {
    counter--


    let seconds = counter % 60
    let minutes = (counter / 60) % 60
    let strMinutes = minutes > 9 ? String(minutes) : "0" + String(minutes)
    let strSeconds = seconds > 9 ? String(seconds) : "0" + String(seconds)
    if seconds > 0 {
        TimerDisplay.text = "\(strMinutes):\(strSeconds)"
    }

    else {
        stopTimer()
        TimerDisplay.text = String("Eggs done!!")
        SoundPlayer.play()
    }

}

@IBAction func ButtonReset(sender: AnyObject) {
    timer.invalidate()
    stopTimer()
    TimerDisplay.text = String("Choose your eggs:")

    ButtonSoft.alpha = 1.0
    ButtonMedium.alpha = 1.0
    ButtonHard.alpha = 1.0
    ButtonSoft.enabled = true
    ButtonMedium.enabled = true
    ButtonHard.enabled = true

}

在运行后台任务方面,我遇到了以下代码示例。

创建后台任务:

func someBackgroundTask(timer:NSTimer) {
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), { () -> Void in
        println("do some background task")

        dispatch_async(dispatch_get_main_queue(), { () -> Void in
            println("update some UI")

        })
    })
}

以下代码行(我认为)使用计时器来运行上述功能:

var timer = NSTimer(timeInterval: 1.0, target: self, selector: "someBackgroundTask:", userInfo: nil, repeats: true)

以下代码将其全部停止:

timer.invalidate()

那么,我将如何根据我的情况调整此方法?如果无法做到这一点,我将如何在我的应用中使用本地通知?

或者我只是放弃了这个应用程序的iPhone版本(我的Apple Watch版本似乎工作正常)。

2 个答案:

答案 0 :(得分:7)

总之,没有。您可以询问后台时间,但最新版本的iOS会给您3分钟的时间。

如果您是背景声音播放应用或导航应用,您可以在后台运行更长时间,但您必须要求这些权限,应用审核委员会将会检查。

最重要的是,第三方不能真正做一个计时器应用程序,倒计时超过3分钟的任意时间。

您可能希望使用定时本地通知。你可以让它们在熄灭时发出声音。在UILocalNotification上的Xcode文档中搜索。

答案 1 :(得分:1)

我通过启动后台任务,然后设置一个不到一分钟的计时器取得了一些成功。当计时器触发时,我启动一个新的后台任务并结束旧任务。我只是继续完成后台任务,每分钟创建一个新任务。

相关问题