0

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)
        }
    }
vadian
  • 274,689
  • 30
  • 353
  • 361
deep ios
  • 9
  • 2
  • Related: https://stackoverflow.com/questions/50375733/background-fetch-performfetchwithcompletionhandler-not-working, https://stackoverflow.com/questions/18315771/performfetchwithcompletionhandler-never-gets-fired – vadian Jun 17 '20 at 09:41

1 Answers1

0

wwdc2019

https://developer.apple.com/videos/play/wwdc2019/707/

The task will be executed only when the system decides to do so

You can check to this forum discussion link too :

https://forums.developer.apple.com/thread/124320

Wahyudimas
  • 197
  • 6