I want to make a timer that start on app load and when it finishes to alert a text and restart the app when alert is closed(when click the ok button of the alert). Have tried with a class timer but I can't send alert and reload the app. I need it to be working in the whole app, and when move to other view not restart it countdown. Is there any easy way without using CocoaPods.
import Foundation
class GlobalTokenTimer {
static let sharedTimer: GlobalTokenTimer = {
let timer = GlobalTokenTimer()
return timer
}()
var internalTimer: Timer?
var jobs = [() -> Void]()
func startTimer(withInterval interval: Double, andJob job: @escaping () -> Void) {
if internalTimer != nil {
internalTimer?.invalidate()
}
jobs.append(job)
internalTimer = Timer.scheduledTimer(timeInterval: interval, target: self, selector: #selector(doJob), userInfo: nil, repeats: true)
}
func stopTimer() {
guard internalTimer != nil else {
print("No timer active, start the timer before you stop it.")
return
}
jobs = [()->()]()
internalTimer?.invalidate()
}
@objc func doJob() {
guard jobs.count > 0 else{return}
}
}