I am trying to run this program on iPhone 3GS with iOS 5. Program works fine on simulator but doesn't work on device. No error but locationManager:didUpdateToLocation:fromLocation delegate method doesn't get called when run on the device.
I just want to print location after every 30 seconds.
I just want to print location after every 30 seconds.
Code:
int main(int argc, char *argv[])
{
iLocation *loc = [[iLocation alloc] init];
NSDate *now = [[NSDate alloc] init];
NSTimer *timer = [[NSTimer alloc] initWithFireDate:now
interval:30
target:loc
selector:@selector(start)
userInfo:nil
repeats:YES];
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
[runLoop addTimer:timer forMode:NSDefaultRunLoopMode];
[runLoop run];
}
@interface iLocation : NSObject <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) NSTimer *timer;
-(void) start;
@end
@implementation iLocation
@synthesize locationManager;
@synthesize timer;
-(void) start
{
NSLog(@"Enter in start");
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
NSLog(@"Exit from start");
}
#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"Enter in didUpdateToLocation");
/*if (startingPoint == nil)
self.startingPoint = newLocation;*/
NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.latitude];
//latitudeLabel.text = latitudeString;
NSLog(@"Latitude = %@", latitudeString);
NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.longitude];
//longitudeLabel.text = longitudeString;
NSLog(@"Longitude = %@", longitudeString);
NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.horizontalAccuracy];
//horizontalAccuracyLabel.text = horizontalAccuracyString;
NSLog(@"Horizontal Accuracy = %@", horizontalAccuracyString);
NSString *altitudeString = [NSString stringWithFormat:@"%gm",
newLocation.altitude];
//altitudeLabel.text = altitudeString;
NSLog(@"Altitude = %@", altitudeString);
NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.verticalAccuracy];
//verticalAccuracyLabel.text = verticalAccuracyString;
NSLog(@"Vertical Accuracy = %@", verticalAccuracyString);
[locationManager stopUpdatingLocation];
NSLog(@"Exit from didUpdateToLocation");
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Enter in didFailWithError");
NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";
NSLog(@"%@", errorType);
NSLog(@"Exit from didFailWithError");
}
@end
Last edited by a moderator: