Reputation: 1786
I've been trying to just get the location coordinates. After I call [locationManager startUpdatingLocation];
nothing happens.
It never calls the - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
Any thoughts?
Notes: I have set the CLLocationManagerDelegate
and made my CLLocationManager
of type strong
. In my viewDidLoad
I set my Delegate and I am trying to use requestAlwaysAuthorization
.
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"locationServicesEnabled: %@", [CLLocationManager locationServicesEnabled] ? @"YES":@"NO");
NSLog(@"locationManager init");
NSLog(@"geocoder init");
locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
//locationManager.delegate = self;
[locationManager setDelegate:self];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
NSLog(@"locationManager startUpdatingLocation");
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"locationManager:manager");
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations;
{
NSLog(@"locations %@", locations);
CLLocation *newLocation = [locations lastObject];
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
// Stop Location Manager
[locationManager stopUpdatingLocation];
// Reverse Geocoding
NSLog(@"Resolving the Address");
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
if (error == nil && [placemarks count] > 0) {
placemark = [placemarks lastObject];
locationLabel.text = [NSString stringWithFormat:@"%@, %@",
placemark.locality,
placemark.administrativeArea];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];
}
ViewController.h
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLGeocoder *geocoder;
@property (strong, nonatomic) CLPlacemark *placemark;
Log
2014-09-24 08:18:39.783 appName[1201:130186] locationServicesEnabled: YES
2014-09-24 08:18:39.784 appName[1201:130186] locationManager init
2014-09-24 08:18:39.784 appName[1201:130186] geocoder init
2014-09-24 08:18:39.785 appName[1201:130186] locationManager startUpdatingLocation
I have also tried removing [locationManager stopUpdatingLocation];
and it still didn't do anything.
Other notes: If I check the device settings/privacy, it indicates that my app does indeed have location permissions. All my .h
properties are @synthesize
properly in the .m
file.
Upvotes: 0
Views: 594
Reputation: 6012
If you are on iOS 8, you need to call either one of these methods :
-[CLLocationManager requestWhenInUseAuthorization]
or -[CLLocationManager requestAlwaysAuthorization]
before calling [locationManager startUpdatingLocation];
first.
You also need to have a description in your .plist for these keys NSLocationUsageDescription
, NSLocationWhenInUseUsageDescription
and NSLocationAlwaysUsageDescription
Upvotes: 3