Search code examples
iosobjective-cswiftnsdate

Find out invalid date in iphone device


When mobile is in invalid date, how can I find programmatically, the changes in iOS device time (whether device is in past/future time)


Solution

  • May be you can use library that can fetch the date and time from a time server.

    Library https://github.com/jbenet/ios-ntp wil do same job for you.

    Usage:

    #import "ios-ntp.h"
    
    @interface ntpViewController : UIViewController <NetAssociationDelegate>
    
    @end
    
    @implementation ntpViewController
    - (void)viewDidLoad {
        [super viewDidLoad];
    
        netAssociation = [[NetAssociation alloc]
        initWithServerName:[NetAssociation ipAddrFromName:@"time.apple.com"]];
        netAssociation.delegate = self;
        [netAssociation sendTimeQuery];
    }
    
    - (void) reportFromDelegate {
        printf("time offset: %5.3f mSec", netAssociation.offset * 1000.0];
    }
    

    Check here for more information and reference:

    https://github.com/jbenet/ios-ntp