22 Nov 2018

To Run a method using Timer in background mode Swift..


If you want to run a code when app is in background state use below code :


   var bgTask = UIBackgroundTaskIdentifier()
        bgTask = UIApplication.shared.beginBackgroundTask(expirationHandler: {

            UIApplication.shared.endBackgroundTask(bgTask)

        })
        let timer = Timer.scheduledTimer(timeInterval: 20, target: self, selector: #selector(notificationReceived), userInfo: nil, repeats: true)
        RunLoop.current.add(timer, forMode: RunLoopMode.defaultRunLoopMode)




@objc  func notificationReceived () {

        Print("Code here")

}

1 comment:

Recent Posts

Codable demo

Link: https://www.dropbox.com/s/kw7c1kgv1628bh7/codableDemo.zip?dl=0