Get Date and Time from Apple Server Get Date and Time from Apple Server ios ios

Get Date and Time from Apple Server


I think that you should be using an Internet time server. They are using a standardized protocol called NTP. The iOS has built-in support for reading NTP server time, but this is not accessible to you as an application developer. Either you implement this yourself, or you could maybe use an open source library, like ios-ntp or HS NTP. I have not used any of them myself. It is probably a good idea to check the position of the device and figure out the timezone, to get a real bulletproof solution.

Here you could read more about the servers and stuff; NIST Internet Time Service

Ideally, you should have a list of servers in the application. If one of them fails, you call the next NTP server in the list.


- (NSDate *) CurrentDate {    if ([self hasInternetConnectivity]) // this tests Internet connectivity based off Apple's Reachability sample code    {        NSSURL * scriptUrl = [NSURL URLWithString: @"http:// <yoursite>. Com / <the 2 line php script>. Php"];        NSData * data = [NSData dataWithContentsOfURL: scriptUrl];        if (data! = nil) {            NSString * tempString = [NSString stringWithUTF8String: [data bytes]];            NSDate * currDate = [NSDate dateWithTimeIntervalSince1970: [tempString doubleValue]];            NSLog (@ "String returned from the site is:% @ and date is:% @", tempString, [currDate description]);           return currDate;             }        else {           NSLog (@ "nsdata download failed");           return [NSDate date];        }    }    else {    NSLog (@ "InternetConnectivity failed");        return [NSDate date];    }}


You can use that open source to get time from default NTP server, or choose your server: https://github.com/huynguyencong/NHNetworkTime

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

And use:

NSDate *networkDate = [NSDate networkDate];