Network Time Protocol for iPhone
Asked Answered
K

7

27

I am writing an application that requires accurate timing. After asking this question, I have decided to investigate using NTP or maybe Simple NTP.

Is there any open source implementation of these protocols for the iPhone? So far I have managed to find a broken link. I am also aware that NTP has a C reference implementation, so I am trying to see how much effort it would require to make it work on the iPhone.

Update: I thought I had a working link, but it is to the Wayback Machine, which only saves text, not files link that works.

Kristofer answered 15/2, 2010 at 22:56 Comment(7)
Don't iPhones use NTP already to set their clocks automatically?Telegenic
@oefe: I don't know how they set their time. Probably from the carrierKristofer
hmm, this would not work in iPod touches... and there are apparently carriers that don't provide time (my old dumbphone sets the clock automatically when I'm in the US, but back in Europe, I have to set the clock manually.Telegenic
You probably want SNTP. The thing to watch for is that NTP usually makes small adjustments to a clock over a long time, with repeated accesses to the time server, so that the computer doesn't get spooked by a large jump in wallclock time (think cron). This would need to run as a background app, which would require a jailbroken iPhone. So, are you asking about a solution for jailbroken iPhones or something you can use in an app in the App Store? Also, IIRC ntpd can also run as a server, which you may not want on an iPhone.Ay
The app is being built for the App StoreKristofer
@Casebash, I cant get what is said on the link. You said "run all this on the iphone, files above". I actually don't know how to run unix commands on iPhone.Overwind
Did you ever find a good solution for this?Antitrust
M
18

There's a small iOS library named ios-ntp. I have used it in one project in the past and it seemed to work reasonably well.

As of mid 2013 the original ios-ntp project has a few missing pieces, so I have mirrored the original svn repository to github. You can checkout out a working version of the code here: https://github.com/jessedc/iOS-ntp

Morissa answered 8/11, 2010 at 4:12 Comment(8)
It says "This is a work in progress." . Can I get the code from somewhere else.?Biannual
Ok, I got the entire xcode project, but when I opened the project, some files where missing. I added the missing files (by downloading them from somewhere) but still no luck. How you guys managed to execute this code .?Biannual
I have mirrored the ios-ntp project to github, making it easy to checkout and get running.Morissa
Anyone know how to make sure that "Network Clock" is really network clock, and not generated by system clock? I have found that when network reachability is low it will just display same time as device clock. It would be better in my case if network clock would fail out completely.Assiduity
Actually if you change the system clock to like next month, the network time will still display same date/hour/minute as system clock, despite leaving network access on. I don't think this is how it's supposed to work???Assiduity
@Assiduity your queries about the code posted are better suited on the issue trackers of the two projects linked above.Morissa
Um I wrote a new class based on ios-ntp. I couldn't use ARC on my project, it just made everything unstable (lots of crashes). Not sure if ARC was responsible for that, or the ios-ntp code itself. I think the UDP socket class it uses tried to access delegates even after they were deallocated, there seemed no way of fixing that. My class sole purpose is to get a recent timestamp (allowing a few seconds even), so it ended up a lot more simple than ios-ntp. It feels stable so far :-)Assiduity
Current version of ios-ntp have an critical error make network time you got is wrong. You can use https://github.com/huynguyencong/NHNetworkTime as a replacementDunlap
R
5

I would probably start by pulling code from OpenNTPD.

Register answered 16/2, 2010 at 0:11 Comment(0)
A
3

If you're not developing a commercial application, peek at the linux source.

http://packages.ubuntu.com/source/lucid/ntp

Aiguille answered 22/2, 2010 at 19:8 Comment(1)
Thanks, but the application is commericalKristofer
H
2

If you need to use NTP, but don't want to trust the iPhone (or iPod's) clock, you can always just use sockets (or Cocoa streams) and query the server yourself. It's one of the simplest network transactions out there.

Cocoa Streams

Honeybunch answered 24/2, 2010 at 21:59 Comment(1)
Beware, though, that network latency can vary wildly over time (depending on network load, signal quality, EDGE/3G vs. WiFi, etc). Probably not a concern if you just need to be accurate to within a few seconds, but more troublesome if you care about milliseconds.Tripitaka
S
2

I don't know what application you're creating, but if trusting the clock is a critical feature of its security or (in the case of game) cheat prevention, etc, then you need to be very careful of open protocols like NTP-- a cheater could do a man-in-the-middle on your NTP request and tell your app what he wanted you to hear.

If this is potentially a concern, you'll want to build your own trustable source of truth for time into your server (e.g. build your own time service, and sign your responses with PK crypto), and set your app's internal clock to that within some acceptable clock skew.

Schram answered 24/2, 2010 at 22:49 Comment(2)
You do need to be careful about trusting the clock. But thats not a reason to not trust NTP. It has crypto modes already for distributing trusted time, so you probably don't need to build your own protocol.Edmea
Why is this down-voted?! I am fixing it. I don't truest the ios system clock, because the user can easy modify and need to check if 30 days elapsed from purchase date or not. Never tought, but very possible to connect with my iphone via wi-fi to internet and there to a proxy to NTP servers and give a fake date for iphone. This is a valid, experienced response. Please upvote it!Nine
A
2

Since you are developing a commercial app, if all else fails, RFC 2030 describes the SNTP protocol. I have no information on commercially available libraries that do not require a background task, which the iPhone won't let you use.

Ay answered 25/2, 2010 at 22:35 Comment(0)
D
1

You can use that open source: https://github.com/huynguyencong/NHNetworkTime

[[NHNetworkClock sharedNetworkClock] syncWithComplete:^{
        NSLog(@"%s - Time synced %@", __PRETTY_FUNCTION__, [NSDate networkDate]);
    }];

And use:

NSDate *networkDate = [NSDate networkDate];
Dunlap answered 20/9, 2015 at 17:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.