I looked for a way to support day and night modes in an upcoming iOS app I’m working on. It is an integral part of the user experience, so ideally, it should “just work” - without requiring any steps on the part of the user.
Core Location
Core Location would prompt and require users to share their precise location (whereas an approximation would do). It also would need access to GPS and/or the Internet. That’s too many moving parts for it to “just work”.
GeoIP
I could put together a web service that tries to geo-locate users by their IP. However, I would not feel comfortable doing so without asking users for their consent first. It would also require an internet connection. In the end, it would be a lot like Core Location - just not as good. Which kind of makes this option ridiculous.
NSTimeZone
This may seem silly at first since places on the northern and southern hemisphere can share the same time zone, yet have very different sunrise and sunset times.
Time zones on iOS, however, usually take the form of “Continent/City” (f.ex. “Europe/Berlin”) and not “GMT+1”. Plus, with default settings, iOS automatically selects and updates the correct time zone based on your actual location. Bingo!
Unfortunately, while NSTimeZone provides the name of a timezone, it does not offer any location information.
So I wrote a category to fill this gap. Based on data from the IANA Time Zone Database, NSTimeZone+ISCLLocation provides CLLocation and ISO 3166 country codes for location-based time zones under iOS and OS X.
Here’s how it works:
In my upcoming app, I'm using the returned location with CLLocation-SunriseSunset to calculate sunrise and sunset times. In cases where a location is not available, I use Core Location as a backup solution.
You can find the NSTimeZone+CLLocation repository on GitHub.
Enjoy!