0

I am still using the xcode 10.2.1 and haven't upgraded to xcode 11 because of some other issues. Now I want to detect that users who are using iOS 13 has chosen dark mode or light mode as their app settings.

As per apple document, If developer build app through previous xcode the app would be in light mode by default, which is my case and is fine.

So, is there a way to detect the user current appearance mode.

There is code snippet which I am using:

if #available(iOS 13.0, *) {
            guard(traitCollection.responds(to: #selector(getter: UITraitCollection.userInterfaceStyle)))
                else { return }

            let style = traitCollection.userInterfaceStyle

            switch style {
            case .light:
                print("light")
            case .dark:
                print("dark")
            case .unspecified:
                print("unspecified")
            @unknown default:
                print("unspecified")
            }

        }

But it is always returning unspecified or light.

  • 2
    if your app build on less than iOS 13 SDK, then will shown on light appearance and will not support dark mode, so you don't need to check until you update Xcode and iOS SDK. – Moayad Al kouz Oct 14 '19 at 11:22
  • Yes, if the SDK is old I can support dark mode. They only reason to know is to know out number of users to find out the severity of updating Xcode. – Safiyan Zulfiqar Oct 14 '19 at 12:47

1 Answers1

2

you can use this property to check if current style is dark mode or not:

if #available(iOS 13.0, *) {
    if UITraitCollection.current.userInterfaceStyle == .dark {
        print("Dark mode")
    }
    else {
        print("Light mode")
    }
}
Enea Dume
  • 3,014
  • 3
  • 21
  • 36