I tried ios-ntp but didn't get expected results, so I found another library which is working fine and really easy to implement.
NHNetworkTime
Pods:
pod 'NHNetworkTime'
pod 'CocoaAsyncSocket'
Simple to use
Import this whenever you want to get time:
#import "NHNetworkTime.h"
Call synchronize in - application: didFinishLaunchingWithOptions: to update time from server when you launch app:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[[NHNetworkClock sharedNetworkClock] synchronize];
return YES;
}
then you can get network time when sync complete in anywhere in your source code:
NSDate *networkDate = [NSDate networkDate];
or add notification to re-update your UI in anywhere you want when time is updated:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkTimeSyncCompleteNotification:) name:kNHNetworkTimeSyncCompleteNotification object:nil];
Cheers !!! :)