解决iOS系统时间被修改的问题

通过http的header获取服务器时间,此方法的延迟较低

NSURL *url = [NSURL URLWithString:@"https://www.baidu.com"];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

[requestsetHTTPMethod:@"POST"];

request.timeoutInterval=0.5;

NSHTTPURLResponse*response =nil;

NSError*error =nil;

[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

if(error !=nil) {

      return0;

}

NSDictionary*httpHeader = [responseallHeaderFields];

NSString*serverDateStr = [httpHeaderobjectForKey:@"Date"];

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

dateFormatter.locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US"];

dateFormatter.timeZone = [NSTimeZone timeZoneWithAbbreviation:@"UTC"];

dateFormatter.dateFormat = @"EEE',' dd MMM yyyy HH':'mm':'ss 'GMT'";

NSDate*serverDate = [dateFormatterdateFromString:serverDateStr];

NSTimeInterval time=[serverDate timeIntervalSince1970];

returntime*1000;

你可能感兴趣的:(解决iOS系统时间被修改的问题)