How can I get the user Location in a simple ViewController in iOS 7?
I have tried in this way, but on debugging I saw that didUpdateLocation
method isn't called:
In MyViewController.h:
#import <CoreLocation/CoreLocation.h>
@interface MyViewController : UIViewController <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
In MyViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
//locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
CLLocation *newLocation = [locations lastObject];
NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"Failed %ld",(long)[error code]);
}
didUpdateLocations
isn't called, but is called didFailWithError
printing in log: "Failed 0". Please help me!