How to automatically turn off a UISwitch at a certain hour ?

Hi, I'm trying to automatically make my app turn off a UISwitch at 8 o'clock , how should I do it ?

Replies

What is the problem ?

- launch an action at 8 pm ?

- have the action turn the switch ?

I'm trying to get the UISwitch to its off state ( for visuals ) and run a function

Not sure you answered my question.


To turn off:


if switch has an IBOutlet

@IBOutlet var mySwitch: UISwitch!


then simply call


mySwitch.isOn = false

But how should I do this automatically every day at 8 o'clock ?

OK, so your question is:

- launch an action at 8 pm ?


If app is in foreground,

https://stackoverflow.com/questions/30944537/ios-how-can-i-schedule-something-once-a-day


if in background:

h ttps://www.hackingwithswift.com/example-code/system/how-to-run-code-when-your-app-is-terminated


Also have a look at what IOS13 (will) allow:

h ttps://medium.com/snowdog-labs/managing-background-tasks-with-new-task-scheduler-in-ios-13-aaabdac0d95b