
0.ntp.png
注意:
ios-ntp這個庫有問題,我最終使用的是通過有效域名獲取網(wǎng)絡(luò)時間??次襠emo中的getInternetDate方法。
我們有時需要獲取一個時間,但如果只是從本地直接獲取有可能因?yàn)槭謾C(jī)系統(tǒng)時間是錯誤(用戶修改了系統(tǒng)時間)的而導(dǎo)致我們獲取到的是一個錯誤的時間。此時就需要我們從網(wǎng)絡(luò)獲取一個正確的時間。
iOS開發(fā)中有人寫了NetworkClock這樣一個類來獲取時間。
github地址:https://github.com/jbenet/ios-ntp
使用:
NSDate * date = [[NetworkClock sharedNetworkClock] networkTime];
NSUInteger timestamp = [NSString stringWithFormat:@"%f", [date timeIntervalSince1970] * 1000].integerValue;
NSLog(@"timestamp:%lu", timestamp);
NSString * dateStr = [date ls_getDateStringWithDateFormat:@"yyyy-MM-dd HH:mm:ss"];
NSLog(@"dateStr:%@", dateStr);
打?。?/p>
6.NetworkClock使用[7060:482670] timestamp:1594631953867
6.NetworkClock使用[7060:482670] dateStr:2020-07-13 17:19:13
通過[[NetworkClock sharedNetworkClock] networkTime]獲取到的是一個NSDate類型數(shù)據(jù),我將其轉(zhuǎn)成了時間字符串進(jìn)行打印。
后期發(fā)現(xiàn)這樣方式是有問題的,大家可以看看demo中的方法:
demo地址:https://github.com/yangguanghei/networkTime
本篇文章到這里就結(jié)束了,愿大家加班不多工資多,男同胞都有女朋友,女同胞都有男朋友。??