AppDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
UIApplication.shared.setMinimumBackgroundFetchInterval(10)
return true
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
if let viewController = window?.rootViewController as? ViewController {
viewController.updateTime()
completionHandler(.newData)
} else {
completionHandler(.noData)
}
}
ViewController:
func updateTime() {
currentTime = Date()
let formatter = DateFormatter()
formatter.timeStyle = .short
if let currentTime = currentTime {
var arr : NSMutableArray = NSMutableArray()
if UserDefaults.standard.value(forKey: "time") != nil {
arr = NSMutableArray(array: UserDefaults.standard.value(forKey: "time") as! NSArray)
}
arr.add(currentTime)
UserDefaults.standard.set(arr, forKey: "time")
UserDefaults.standard.synchronize()
Info_LB.text = formatter.string(from: currentTime)
}
}