Yes, you can. I just tested and it works. You just need to add your timer to the main run cycle of NSRunLoopCommonModes:
RunLoop.main.add(yourTimerName, forMode: .commonModes)
import NotificationCenter class TodayViewController: UIViewController, NCWidgetProviding { @IBOutlet weak var strTimer: UILabel! var timer = Timer() func updateInfo() { strTimer.text = Date().description } override func viewDidLoad() { super.viewDidLoad() timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(updateInfo), userInfo: nil, repeats: true) RunLoop.main.add(timer, forMode: .commonModes) } func widgetPerformUpdate(completionHandler: @escaping (NCUpdateResult) -> Void) { completionHandler(.newData) } }
source share