关闭/在后台加上发送通知时,应用程序中的时间会停止

时间:2017-10-18 16:52:23

标签: ios swift xcode background

我希望我的应用程序在应用程序进入后台后继续倒计时,当它达到某个数字时,让我们说还剩10个小时,它会向用户发送一条通知说明

这是我目前使用的代码 我用谷歌搜索了一些东西,但是到目前为止它似乎太过先进了,以及应用程序保存和稍后检查时间,是否有任何好的教程,或者有人知道我该如何解决这个问题?

class ViewController: UIViewController {


 public var streakNumber = 0

 public var activated = false


//Labels
@IBOutlet var streakNumberLabel: UILabel!

@IBOutlet var remainingTimeTextLabel: UILabel!

@IBOutlet var remainingTimeNumberLabel: UILabel!
//Buttons
@IBAction func startButton(_ sender: Any) {
    Activate()

}
@IBAction func stopButton(_ sender: Any) {
   // Deactivate()
}

//Timer
var timer = Timer()
var seconds = 59
var minutes = 59
var hours = 47


//END
override func viewDidLoad() {

    super.viewDidLoad()


    // Do any additional setup after loading the view, typically from a nib.
}



override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
func Activate (){

    if activated == false {
        streakNumber += 1
    }
    activated = true
    streakNumberLabel.text = String(streakNumber)
}
func Deactivate (){
    ActivTimer()

    if activated == true{
        activated = false
    }
}
@objc func Clock(){
    seconds = seconds-1
    if seconds == -1{
        seconds = 59
        minutes = minutes-1
    }
    if minutes  == -1{
        minutes  = 59
        hours = hours-1
    }
    remainingTimeNumberLabel.text = (String(hours) + ":" + String(minutes) + ":" + String(seconds))
    if seconds == 0 && hours == 0 && minutes == 0 {

        timer.invalidate()

    }
}
func ActivTimer(){
     timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(ViewController.Clock), userInfo: nil, repeats: true)
}

 }

0 个答案:

没有答案