3

I'm using this method in order to get the timeZone from lat/long.

-(void)getTimeZoneFromLatLong
{
    CLLocation *location = [[CLLocation alloc] initWithLatitude:self.parentVC.currentCity.latitude.doubleValue longitude:self.parentVC.currentCity.longitude.doubleValue];
    CLGeocoder *geoCoder = [[CLGeocoder alloc]init];
    [geoCoder reverseGeocodeLocation: location completionHandler:^(NSArray *placemarks, NSError *error)
    {
        CLPlacemark *placemark = [placemarks objectAtIndex:0];
        _placemark = placemark;
    }];
}

Then I can use the timeZone in order to call :

EDSunriseSet *eds = [EDSunriseSet sunrisesetWithTimezone:_placemark.timeZone
           latitude:latitude.doubleValue longitude:longitude.doubleValue];

EDSunriseSet is a library in order to get Sunrise/Sunset values from lat/long and timeZone.

It's working perfectly, however Crashlytics is alerting me that [CLPlacemark timeZone] is incompatible with iOS8 and lower.

How can I adapt my code for iOS8 ?

EDIT: [_placemark timezone] and _placemark.timezone are both accepted ?

Krunal
  • 77,632
  • 48
  • 245
  • 261
  • http://stackoverflow.com/questions/15417176/how-to-get-the-time-zone-name-for-a-cllocation ? – Larme Feb 23 '17 at 09:50
  • That's exactly what I'm doing –  Feb 23 '17 at 09:54
  • 1
    No, you didn't read correctly. That question is prior to iOS9. They are retrieving the NSTimeZone from its loc/lat witouth using `timeZone` property of `CLPlaceMark` (because it didn't existed at that time). – Larme Feb 23 '17 at 09:59
  • I can't close the question. I need answers. I used chris'solution from your link. –  Feb 23 '17 at 10:10

1 Answers1

2

It's available from iOS 9.

/*
     *  timeZone
     *
     *  Discussion:
     *      Returns the time zone associated with the placemark.
     */
    @available(iOS 9.0, *)
    open var timeZone: TimeZone? { get }

For iOS 8, try this

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 %@",substr); 
}
Krunal
  • 77,632
  • 48
  • 245
  • 261
  • Yeah my question means how can I achieve what I'm doing but for iOS 8. –  Feb 23 '17 at 09:48
  • You want to set timeZone for IOS 8 also? – Krunal Feb 23 '17 at 09:49
  • I want to get timeZone from lat/long for iOS 8 yes. My code is working for iOS 9 and higher. –  Feb 23 '17 at 09:50
  • There are two ways to do that in iOS 8.
    1st is
    [Find a library here] https://github.com/Alterplay/APTimeZones and CLLocation *location = [[CLLocation alloc] initWithLatitude:100.12345 longitude:80.12345]; NSTimeZone *timeZone = [[APTimeZones sharedInstance] timeZoneWithLocation:location]; NSLog(@"%@", timeZone);
    – Krunal Feb 23 '17 at 09:59
  • 2nd is CLLocation *currentLocaiton = [[CLLocation alloc] initWithLatitude:latitude longitude:longitude]; [geoCoder reverseGeocodeLocation:currentLocaiton completionHandler:^(NSArray *placemarks, NSError *error) { – Krunal Feb 23 '17 at 09:59
  • 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 %@",substr); } – Krunal Feb 23 '17 at 10:00
  • write an answer if you want to have the accepted answer. –  Feb 23 '17 at 12:11
  • Are facing any other problem? I hope this should work for you. if not then reply here, I'll help to find another solution, that can solve your problem. – Krunal Feb 23 '17 at 12:14
  • I used the second solution you provided. it's working. –  Feb 23 '17 at 12:31
  • OK.. Updated actual answer – Krunal Feb 23 '17 at 14:12