I’ve a navigation app. Throughout a navigation session, I stop the display screen from sleeping. When customers are driving/navigating round dawn or sundown, look ought to change between mild and darkish mode.
With a view to do that, I created a subclass of UIWindow:
class WindowWithDarkModeMonitor: UIWindow {
override func traitCollectionDidChange(_ previousTraitCollection: UITraitCollection?) {
tremendous.traitCollectionDidChange(previousTraitCollection)
print("traitCollectionDidChange")
}
}
traitCollectionDidChange
is meant to be referred to as at any time when traits (like portrait/panorama and darkish/mild mode change). (Apple Documentation (b.t.w. I do know this technique will probably be deprecated, however my app must assist iOS16 for the second, so I nonetheless want it).
Now, the issue is, traitCollectionDidChange
will not be referred to as on a scheduled look change. It IS referred to as after I change between darkish and lightweight mode by the management heart. It is also referred to as after I rotate my machine.
I browsed a LOT, and have tried a LOT, however cannot discover a resolution. This SO query comes shut, however the prime (and solely) reply does not work for my case both.
After some testing, I really came upon that no app I do know of modifications from darkish to mild or vice versa when it is lively within the foreground on the time of the scheduled change. You’ll be able to do this your self:
- Go to iOS Settings > Show & Brightness
- Set look to AUTOMATIC and for Choices select ‘Customized Schedule’ and select the occasions so {that a} change is 1 or 2 minutes away.
- Now open Apple Mail, or Safari, or some other app that helps darkish mode
- Be certain your show does not fall asleep, and wait till the minute passes the place the darkish/mild change ought to happen.
- Nothing occurs!!!
- The darkish/mild change is barely utilized after your iPhone is auto-locked or locked manually, and unlocked once more.
I’ve a really sturdy feeling that is both an iOS bug (or a ‘function’?). In any case, it could be unlucky for an app like mine, if I could not use the system’s automated darkish/mild change at sundown/dawn. I must construct my very own resolution to find out if it is day or night time (primarily based on location and time zone). Not unimaginable, however I would fairly not re-invent the wheel, and hold my app as near system conduct as doable.
So, is there any strategy to make the system name traitCollectionDidChange
upon a scheduled look change (darkish/mild mode change)? Or, some other strategy to get notified about this?