0

I have a page on my app that runs a counter when the button is pressed. It stops running when I go to another page or when the app is completely terminated via double tapping home and swiping it away. That's fine with me and exactly what i want it to do. I would also like the counter to stop when i just press the home button even if the app is running still, I want the counter to stop. Ill post my counter below.

class Miner: UIViewController {

//Start
@IBOutlet weak var startMining: UIButton!

//Coin Label
@IBOutlet weak var coinLabel: UILabel!

//Counter
var count:Int {
    get {
        return UserDefaults.standard.integer(forKey: "count")
    }
    set {
        UserDefaults.standard.set(newValue, forKey: "count")
        coinLabel.text = "Coins: 0. \(newValue)"

    }
}
var counting:Bool = false
var timer:Timer = Timer()




override func viewDidLoad() {


    super.viewDidLoad()


}

@objc func counter() -> Void {

    count += 1
    coinLabel.text = "Coins: 0." + String(count)
    walletLabel.text = "Wallet: 0." + String(count)

}

@IBAction func startMining(_ sender: Any) {
    if counting {
        // Stop Counting
        startMining.setTitle("Start", for: .normal)

        timer.invalidate()

        counting = false


    } else if !counting {
        // Start Counting
        startMining.setTitle("Stop", for: .normal)
        // Start timer
        timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector (counter), userInfo: nil, repeats: true)
        counting = true
    }



}

}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
Collin
  • 27
  • 5
  • 1
    The timer will stop as soon as your app is suspended, which happens shortly after the home button is pressed. Do you mean that you want the user to have to restart the timer manually when they return to the app? – Paulw11 Dec 23 '17 at 00:31
  • Use the notification with .UIApplicationWillEnterForeground – El Tomato Dec 23 '17 at 00:33
  • How shortly after? When the timer is running and i press home and then click the app again after 15 - 30 seconds the timer is still running. I want it to stop running anytime i leave the app. – Collin Dec 23 '17 at 00:35
  • never mind i see what you're saying. I just wasn't giving it enough time to "suspend" okay thank you so much. – Collin Dec 23 '17 at 00:36
  • @Collin you should use will resign active notification https://developer.apple.com/documentation/foundation/nsnotification.name/1622973-uiapplicationwillresignactive – Leo Dabus Dec 23 '17 at 03:28
  • Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. **Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.** – Leo Dabus Dec 23 '17 at 03:35

1 Answers1

1
override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(appDidEnterBackground), name: .UIApplicationDidEnterBackground, object: nil)
}

func appDidEnterBackground() {
    // stop counter
}
yakovlevvl
  • 524
  • 3
  • 12