skip to Main Content

In my application I need to make sure, while using the app, the user is using the default time zone and time on his device.
I have some time-sensitive information and I can’t rely on network timing because the app works offline (without Internet).

I have tried:

TimeZone.current != TimeZone.autoupdatingCurrent

It’s not giving me the result I am expecting. Please suggest some solution for this.

2

Answers


  1. struct ContentView: View {
    var body: some View {
        Text("Default Time: (defaultTime)")
        Text("Default Time Zone: (defaultTimeZone)")
    }
    
    var defaultTime: String {
        let calendar = Calendar.current
        let now = Date()
        let timeFormatter = DateFormatter()
        timeFormatter.dateFormat = "hh:mm a"
        let defaultTime = timeFormatter.string(from: now)
        return defaultTime
    }
    
    var defaultTimeZone: String {
        let defaultTimeZone = TimeZone.current.identifier
        return defaultTimeZone 
      }
    }
    
    Login or Signup to reply.
  2. You absolutely cannot verify that the current device time is "correct," which is what I believe you’re trying to do. Even if you could check the user’s explicit settings using some undocumented trick, it wouldn’t help. All the user needs to do is turn on airplane mode, set the time to what they want, then set it to "automatic." The time will continue to be what they set until the device can reconnect to the network.

    There are some things you can do. You can observe .significantTimeChangeNotification to discover when the clock has changed in certain cases, even in the background. Keep in mind this is called for several reasons, most completely appropriate, including once a day at midnight.

    You can keep track of what time it is when the app goes into the background and make sure that time always moves forward, or at least does not significantly move backwards (watch out for small adjustments when moving between cell towers).

    You can keep track of systemUptime to look for cases where the system clock has not progressed as much as system uptime. Rebooting the phone will bypass this check, but it should never cause a false positive.

    It’s never possible to know that the clock is "correct" in any meaningful way, but there are ways to determine that it is suspicious.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search