Saturday, 10 September 2016

objective c - Location Manager in IOS 8 Not working




I tried displaying the current location of the user (I mainly need the zip code,) and nothing happens when I launch the app, the app just displays empty labels without a crash message.
Here is my code:



@implementation ViewController {

CLLocationManager *locationManager;
CLGeocoder *geocoder;
CLPlacemark *placemark;
}

@synthesize array, mytableView, latitudeLabel, longitudeLabel, addressLabel;

- (void)viewDidLoad {
[super viewDidLoad];


locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8) {
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
geocoder = [[CLGeocoder alloc] init];
}


- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
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 didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;

if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}

// Stop Location Manager

[locationManager stopUpdatingLocation];

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];
addressLabel.text = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,

placemark.administrativeArea,
placemark.country];
} else {
NSLog(@"%@", error.debugDescription);
}
} ];

}

- (IBAction)getCurrentLocation:(id)sender {

locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;

[locationManager startUpdatingLocation];

}

Answer



Add these properties in your plist file. The value of string could be anything you want.




enter image description here



Edited to further answer another query of Mike in the comment:



In your viewcontroller's .h file



@interface YourViewController : UIViewController

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...