1

This is the scenario:

I'm writing a medical related program, that would be use while with no connection. When some action act, the program would write the time to CoreData record.

That's the problem, if their device set the time to a diff time like earlier than the real time. That would be a big problem coz it's for medical usage.

  • So, how can i get the "real" time even if there is no connection?
  • Or, is it possible to disallow user changing the device time using something like restrictions or DeviceProfile?
mineschan
  • 473
  • 5
  • 15
  • Give us a bit of background and let us know why and we'll be able to suggest some viable alternatives to requiring time – Dan Hanly Mar 26 '12 at 14:49

4 Answers4

2

It's only because Apple IS the Big Brother they claimed to be fighting in 1984. Welcome to 1984! Otherwise we would have access to real time time, and an English version of ISO date format! :-/ Every iPwn, and now other devices, has a GPS receiver and an Internet capability, from which sub-second accurate time could be derived, yet Apple insists on forcing us to depend on AT&T to automatically set our clocks. It's only recently that AT&T started delivering accurate time, thank god for small favors.

The lack of GPS and NTP time setting, plus the glaring omission of ISO 8601 date formatting with otherwise USA formats and language, is extremely annoying on a daily basis.

So, the answer to your question is, Yes, it is feasible, but not in Apple's Jail, since you cannot set RTC from GPS or NTP without jail breaking.

PS: my guess is that AT&T insists on this for call-timing or something stupid that has to do with charging us more money! ;-)

captain
  • 44
  • 1
  • "Today we celebrate the first glorious anniversary of the Information Purification Directives. We have created for the first time in all history a garden of pure ideology, where each worker may bloom, secure from the pests of any contradictory true thoughts...." ([ref](http://www.hep.uiuc.edu/home/g-gollin/1984_commercial_dialogue.html), [ref](http://rockcheetah.com/blog/humor/big-brother-1984-apple-macintosh-ad-steve-jobs/)) – JohnK Jul 10 '13 at 00:52
  • If there is possible, How could we do this?. Is there any example?. – sathiamoorthy Dec 07 '15 at 11:04
1

You get the real time from [NSDate date]. For example, the following:

[[NSDate date] timeIntervalSince1970]

gives the number of seconds since 1/1/1970 midnight UTC. This is independent of timezone and independent of whatever time is set by the user. If you know the timezone, then you can convert that to local time with the NSDateFormatter if you like, but make sure to also record the timezone to make the representation unique.

EDIT: Sorry, this answer is actually not correct. After trying it out, it appears that setting the time by the user also changes the NSDate values.

fishinear
  • 6,101
  • 3
  • 36
  • 84
  • What if the user was to manually change the system time in the settings? This is often why time dependant games such as Farmville require an internet connection. Oh, these crops aren't going to be done for another 5 hours? No worries I'll just adjust the system time to five hours ahead! – Dan Hanly Mar 26 '12 at 13:36
  • I just tried it out, and you are actually correct. I did not think that Apple would make such a beginners mistake... Will adapt my answer to reflect that. – fishinear Mar 26 '12 at 14:30
  • It's not really a beginners mistake, there's actually no realistic way to judge the time without an internet connection as a reference. Unless they added a 100% accurate clock system to the hardware! – Dan Hanly Mar 26 '12 at 14:43
  • Every phone actually has a real time clock built in, which is close to 100% accurate, and which keeps running even if you switch off the phone. IOS is based on Unix, and ever since 1970, Unix real time clocks have been running on GMT time, only to be adapted by the NTP protocol that synchronizes all clocks over the internet. Local time, even if set by the user, is then derived by an offset from the real time clock time. So, after a Unix device once has had a connection to the internet, the real time clock should always run on GMT time. – fishinear Mar 26 '12 at 14:55
  • Yeah, exactly, so if your user isn't in the GMT timezone, then the hardware clock isn't accurate unless you have an internet connection to synchronise the two! – Dan Hanly Mar 26 '12 at 15:03
  • 1
    BTW, there is a NSSystemClockDidChangeNotification notification that you could use to at least get notified when the time changes. You would not have an accurate time in that case, but you could record that time may have been tempered with. – fishinear Mar 26 '12 at 15:08
  • With GMT time, I mean counting seconds since 1/1/1970 midnight GMT. That is an absolute timestamp, independent of timezones. – fishinear Mar 26 '12 at 15:10
  • 1
    I would use NSSystemClockDidChangeNotification to trigger an Alert to say "OI! You cheated!" – Dan Hanly Mar 26 '12 at 15:56
  • NSSystemClockDidChangeNotification may be helpful to me! LOL, thanks bro. – mineschan Apr 23 '12 at 11:43
0

So, how can i get the "real" time even if there is no connection?

If by "no connection" you mean "no network connection of any sort" the answer is that you can't.

I think the best you can do is disable the functionality if you can't find a way to independently verify the system time (and tell the user why).

JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • if I don't care of using private API, if there any possibility to know if the device is set to auto time correction – mineschan Mar 26 '12 at 11:32
0

NSDate and all associated classes read the date from the system time. Without an internet connection to refer to, the NSDate class is open to user abuse.

I used an example in a comment on fishinear's answer of Farmville. If the user plants some crops that take five hours to grow, you can just change the system time to five hours in the future to harvest. Which, I'm sure, is one of the reasons that Zynga requires an internet connection to play their games.

Without some time-telling hardware, Apple cannot realistically tell time without an internet connection; even if they did have some amazing solution, they'd have to take into account timezones and even travel across timezones in order to make this work.

If I were you, I'd require an internet connection at specified intervals (once a day or the like) in order to draw a reference.

Let us know why you need this and we may be able to suggest some viable alternatives in function.

Dan Hanly
  • 7,829
  • 13
  • 73
  • 134
  • The real time is actually present in the device, as I discuss [here](http://stackoverflow.com/questions/1444456/is-it-possible-to-get-the-atomic-clock-timestamp-from-the-iphone-gps/17413277#17413277). [The question](http://stackoverflow.com/questions/17560195/is-there-any-way-to-get-the-tamper-proof-date-and-time-on-iphone) is how to get access to it. – JohnK Jul 10 '13 at 01:15