我需要根据应用中的给定纬度/经度显示日期和时间。我使用Google API来获取时区ID,但它也应该脱机工作。有没有办法在没有连接互联网的情况下获得时区?
答案 0 :(得分:3)
试试这个:
CLLocation *location = [[CLLocation alloc] initWithLatitude:50.449846
longitude:30.523629];
NSTimeZone *timeZone = [[APTimeZones sharedInstance] timeZoneWithLocation:location];
NSLog(@"%@", timeZone);
在此处查找图书馆:https://github.com/Alterplay/APTimeZones
您也可以通过代码(由https://stackoverflow.com/a/27054583/3202193回答)来执行此操作:
CLLocation *currentLocaiton = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude];
[geoCoder reverseGeocodeLocation:currentLocaiton completionHandler:^(NSArray *placemarks, NSError *error) {
if (error == nil && [placemarks count] > 0) {
placeMark = [placemarks lastObject];
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"identifier = \"[a-z]*\\/[a-z]*_*[a-z]*\"" options:NSRegularExpressionCaseInsensitive error:NULL];
NSTextCheckingResult *newSearchString = [regex firstMatchInString:[placeMark description] options:0 range:NSMakeRange(0, [placeMark.description length])];
NSString *substr = [placeMark.description substringWithRange:newSearchString.range];
NSLog(@"timezone id %@",substr);
}