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

Mon*_*eth 3 ios swift

我是iOS开发的新手,我正在开发一款iPhone烹饪应用程序,让用户可以选择三种"计时器"选项.第一个计时器运行6分钟,第二个计时器运行8.5分钟,最后一个计时器运行11分钟.

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

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

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

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


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

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

例如,用户在正好下午12:00点选择"煮熟的鸡蛋"按钮,应用程序启动计数器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

}
Run Code Online (Sandbox Code Playgroud)

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

要创建后台任务:

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

        })
    })
}
Run Code Online (Sandbox Code Playgroud)

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

var timer = NSTimer(timeInterval: 1.0, target: self, selector: "someBackgroundTask:", userInfo: nil, repeats: true)
Run Code Online (Sandbox Code Playgroud)

以下代码可以阻止它:

timer.invalidate()
Run Code Online (Sandbox Code Playgroud)

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

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

Dun*_*n C 7

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

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

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

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