-1

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!

Mahendra Liya
  • 12,912
  • 14
  • 88
  • 114
Pinturikkio
  • 1,490
  • 1
  • 14
  • 28
  • Have you checked `[CLLocationManager locationServicesEnabled]` and `[CLLocationManager authorizationStatus]`? – neilco Dec 16 '13 at 13:20
  • Yes [CLLocationManager locationServicesEnabled] is 1 and [CLLocationManager authorizationStatus] is 3 – Pinturikkio Dec 16 '13 at 13:54
  • According to `CLError.h` error code 0 is `kCLErrorLocationUnknown`. Are you running this on a device or in the simulator? – neilco Dec 16 '13 at 14:01
  • The problem was the simulator that once time doesn't simulate location, once time simulate location! – Pinturikkio Dec 16 '13 at 14:12

2 Answers2

2

Firstly check that you actually have a valid WiFi and 3G connection

then

1) go to settings and reset your location services

2) reset your network settings

Also check is the default location set if its in a simulator. This link will show that.

Community
  • 1
  • 1
Nithin Michael
  • 2,166
  • 11
  • 32
  • 56
1

CLLocation manager says

kCLAuthorizationStatusNotDetermined = 0, // User has not yet made a choice with regards to this application
kCLAuthorizationStatusRestricted,        // This application is not authorized to use location services.  Due
                                         // to active restrictions on location services, the user cannot change
                                         // this status, and may not have personally denied authorization
kCLAuthorizationStatusDenied,            // User has explicitly denied authorization for this application, or
                                         // location services are disabled in Settings
kCLAuthorizationStatusAuthorized         // User has authorized this application to use location services

In your case CLAuthorizationStatus 0 means you not allow app to access location services.

Bhumeshwer katre
  • 4,671
  • 2
  • 19
  • 29