For a project I need to use CoreLocation services but from another language. However, the problem is an infinite NSRunLoop. I tried to use observers but without any success. I don't get anything in
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
So I wait for location updates in the run loop. I could use runUntilDate but I need to be sure that a user will click Ok in next 3/5/10 seconds.
So, here is my code:
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
@interface Location : NSObject <CLLocationManagerDelegate>
@property (nonatomic, retain)CLLocationManager *manager;
@property (nonatomic, retain)NSTimer *timer;
@end
@implementation Location
- (instancetype)init {
if (self = [super init]) {
_manager = [[CLLocationManager alloc] init];
_manager.delegate = self;
}
return self;
}
- (void)dealloc {
[_manager release];
[_timer release];
[super dealloc];
}
- (void)launch
{
[_manager startUpdatingLocation];
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1
target:self
selector:@selector(checkIfUpdated:)
userInfo:nil
repeats:YES];
[_timer release];
[[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
}
- (void)checkIfUpdated:(NSTimer *)timer
{
if (_manager.location != nil) {
[timer invalidate];
[timer release];
NSLog(@"invalidate the timer %@", timer);
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Error %@", error.userInfo);
[_timer invalidate];
[_timer release];
}
@end
int main(int argc, const char * argv[]) {
Location *location = [[Location alloc] init];
[location launch];
NSString *str = [NSString stringWithFormat:@"%f, %f", location.manager.location.coordinate.latitude,
location.manager.location.coordinate.longitude];
[str release];
NSLog(@"%s", [str UTF8String]);
return 0;
}
Thank you in advance. Cheers