Location Manager is not updating location - ios

I have the following code:
- (void) viewWillAppear {
[self startSignificantChangeUpdates];
}
- (void)startSignificantChangeUpdates
{
if ([CLLocationManager locationServicesEnabled])
{
NSLog(#"start significant changes");
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}
}
problem is that the location manage is not calling its delegate function
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
What can I do?

In your .h file:
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController <CLLocationManagerDelegate>
In your .m file:
#implementation ViewController {
// This is a private variable, used within this file only
CLLocationManager *locationManager;
}
-(void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
}
-(void)viewWillAppear {
// If the delegate is still not being set, try putting this code into viewDidAppear
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
-(void)locationManger:(CLLocationManager *)manager didFailWithError:(NSError *)error {
NSLog(#"didFailWithError: %#", error);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
NSLog(#"didUpdateToLocation: %#", [locations lastObject]);
CLLocation *currentLocation = [locations lastObject];
[locationManager stopUpdatingLocation];
}
For more information about getting the user's location, look at this guide, which I used to implement location into an app of mine: http://www.appcoda.com/how-to-get-current-location-iphone-user/

How are you testing this? If you are running this in the simulator, you can go to Debug->Location->Freeway Drive. You can put a break point in locationManager:didUpdateToLocation method or even put a log statement to log the current location coordinates. You should see it work.

Please replace this line
[locationManager startMonitoringSignificantLocationChanges];
with this code
[locationController.locationManager startUpdatingLocation];
and then check
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
}

Related

current location alway 00000.00, 00000.00 [duplicate]

I have to find the current location and i am using this code
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
- (void)getCurrentLocation{
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"Latitude = %#", latitude);
NSLog(#"Longitude = %#", longitude);
}
but latitude and longitude is coming zero? and i have called these method in viewDidLoad
[self getCurrentLocation];
[self getLocation];
why is it so please help me. thanks
I hope , Following info can help you:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface yourController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
#end
and add this:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
Callback function
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(#"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
You also have to add a string for the
[`NSLocationAlwaysUsageDescription`]
You need to use "CLLocationManagerDelegate".
Just go through this tutorial. How To Get the User Location in iPhone App
Here you will learn about CoreLocation Framework.
This is the delegate method which will return you current lat-long.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
And also make sure your app authorize to use location services.
And Also run your app on device, or if you want to see the result in simulator, follow the steps mentioned in tutorial.
Step 1 : #import <CoreLocation/CoreLocation.h> import framework. Also add framework in your project.
Step 2 : Make sure your view-controller implement CLLocationManagerDelegate
#interface MyLocationViewController : UIViewController <CLLocationManagerDelegate>
Step 3 : Define class instance of CLLocationManager
#implementation MyLocationViewController {
CLLocationManager *locationManager;
}
Step 4 :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
if([locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
//iOS 8.0 onwards
[locationManager requestAlwaysAuthorization];
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
Step 5 : Here is your CLLocationManagerDelegate
method implementation
- (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) {
NSLog(#"Longitude %.8f",currentLocation.coordinate.longitude);
NSLog(#"Latitude %.8f",currentLocation.coordinate.latitude);
}
}
First go to your info.plist and right click on it and select the source code option.
Then add the following lines after the <dict> keyword
<key>NSLocationAlwaysUsageDescription</key>
<string>This App wants to Know your location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This App wants to Know your location</string>
Without this, you can't get CLLocation working in iOS 9.0.
Now follow the instruction.
Import
#import <CoreLocation/CoreLocation.h>
and list the CoreLocation delegate like
#interface ViewController ()<CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
Then in the viewDidLoad method add the following lines-
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
Now implement the delegate methods
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Sorry"
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) {
NSLog(#"%#", [NSString stringWithFormat:#"%.4f", currentLocation.coordinate.longitude]);
NSLog(#"%#", [NSString stringWithFormat:#"%.4f", currentLocation.coordinate.latitude]);
}
}
It takes some time for the CLLocationManager to get the coordinates. Therefore, the CLLocationManager uses a delegate to notify when it received the location updates. Therefore, you cannot get the result immediately. Please have a look at this post to see how it's done.
Well, I wanted you to read the doc, still I am posting the solution.
-(void)viewDidLoad{
[self initLocationManager];
}
-(void)initLocationManager{
self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate=self;
if (([_locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])) {
[_locationManager requestWhenInUseAuthorization];
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
}
Then handle the location updates in the CLLocationManager delegate method like below
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
NSLog(#"%f,%f",location.coordinate.latitude,location.coordinate.longitude);
}
}
Update
To make it work, you need to do two things
Enable Background Modes from Capabilities Tab, and choose Location Updates, see image below
Add NSLocationWhenInUseUsageDescription in Info Tab, into the plist, see image
It will start working.
Hope it helps. Cheers.
Use this code it works
firstly add NSLocationAlwaysUsageDescription as string in info.plist
in .h
#import <CoreLocation/CoreLocation.h>
#interface ExamListVC : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager * locationManager;
}
#end
in .m use this code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
locationManager . delegate = self;
[self initLocationManager];
}
-(void)initLocationManager
{
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
if (([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]))
{
[locationManager requestWhenInUseAuthorization];
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0)
{
NSLog(#"Current Coordinates are %f,%f",location.coordinate.latitude,location.coordinate.longitude);
[locationManager stopUpdatingLocation];
}
}
it will give output like
[2390:96060] Current Coordinates are 37.332331,-122.031219

Why latitude and longitude is showing zero in ios 9?

I have to find the current location and i am using this code
-(CLLocationCoordinate2D) getLocation{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
return coordinate;
}
- (void)getCurrentLocation{
CLLocationCoordinate2D coordinate = [self getLocation];
NSString *latitude = [NSString stringWithFormat:#"%f", coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:#"%f", coordinate.longitude];
NSLog(#"Latitude = %#", latitude);
NSLog(#"Longitude = %#", longitude);
}
but latitude and longitude is coming zero? and i have called these method in viewDidLoad
[self getCurrentLocation];
[self getLocation];
why is it so please help me. thanks
I hope , Following info can help you:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#interface yourController : UIViewController <CLLocationManagerDelegate> {
CLLocationManager *locationManager;
}
#end
and add this:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
[self.locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
Callback function
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(#"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
}
You also have to add a string for the
[`NSLocationAlwaysUsageDescription`]
You need to use "CLLocationManagerDelegate".
Just go through this tutorial. How To Get the User Location in iPhone App
Here you will learn about CoreLocation Framework.
This is the delegate method which will return you current lat-long.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
And also make sure your app authorize to use location services.
And Also run your app on device, or if you want to see the result in simulator, follow the steps mentioned in tutorial.
Step 1 : #import <CoreLocation/CoreLocation.h> import framework. Also add framework in your project.
Step 2 : Make sure your view-controller implement CLLocationManagerDelegate
#interface MyLocationViewController : UIViewController <CLLocationManagerDelegate>
Step 3 : Define class instance of CLLocationManager
#implementation MyLocationViewController {
CLLocationManager *locationManager;
}
Step 4 :
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
locationManager = [[CLLocationManager alloc] init];
if([locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
//iOS 8.0 onwards
[locationManager requestAlwaysAuthorization];
}
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
Step 5 : Here is your CLLocationManagerDelegate
method implementation
- (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) {
NSLog(#"Longitude %.8f",currentLocation.coordinate.longitude);
NSLog(#"Latitude %.8f",currentLocation.coordinate.latitude);
}
}
First go to your info.plist and right click on it and select the source code option.
Then add the following lines after the <dict> keyword
<key>NSLocationAlwaysUsageDescription</key>
<string>This App wants to Know your location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This App wants to Know your location</string>
Without this, you can't get CLLocation working in iOS 9.0.
Now follow the instruction.
Import
#import <CoreLocation/CoreLocation.h>
and list the CoreLocation delegate like
#interface ViewController ()<CLLocationManagerDelegate>
#property (strong, nonatomic) CLLocationManager *locationManager;
#end
Then in the viewDidLoad method add the following lines-
self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate=self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]){
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
Now implement the delegate methods
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:#"Sorry"
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) {
NSLog(#"%#", [NSString stringWithFormat:#"%.4f", currentLocation.coordinate.longitude]);
NSLog(#"%#", [NSString stringWithFormat:#"%.4f", currentLocation.coordinate.latitude]);
}
}
It takes some time for the CLLocationManager to get the coordinates. Therefore, the CLLocationManager uses a delegate to notify when it received the location updates. Therefore, you cannot get the result immediately. Please have a look at this post to see how it's done.
Well, I wanted you to read the doc, still I am posting the solution.
-(void)viewDidLoad{
[self initLocationManager];
}
-(void)initLocationManager{
self.locationManager=[[CLLocationManager alloc] init];
self.locationManager.delegate=self;
if (([_locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])) {
[_locationManager requestWhenInUseAuthorization];
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
}
Then handle the location updates in the CLLocationManager delegate method like below
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
// If it's a relatively recent event, turn off updates to save power.
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0) {
NSLog(#"%f,%f",location.coordinate.latitude,location.coordinate.longitude);
}
}
Update
To make it work, you need to do two things
Enable Background Modes from Capabilities Tab, and choose Location Updates, see image below
Add NSLocationWhenInUseUsageDescription in Info Tab, into the plist, see image
It will start working.
Hope it helps. Cheers.
Use this code it works
firstly add NSLocationAlwaysUsageDescription as string in info.plist
in .h
#import <CoreLocation/CoreLocation.h>
#interface ExamListVC : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager * locationManager;
}
#end
in .m use this code
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
locationManager . delegate = self;
[self initLocationManager];
}
-(void)initLocationManager
{
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
if (([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]))
{
[locationManager requestWhenInUseAuthorization];
}
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 15.0)
{
NSLog(#"Current Coordinates are %f,%f",location.coordinate.latitude,location.coordinate.longitude);
[locationManager stopUpdatingLocation];
}
}
it will give output like
[2390:96060] Current Coordinates are 37.332331,-122.031219

Xcode 6 why CLLocationManager didn't return latitude and longitude

Why I can get the direction but I can't get the latitude and longitude?
Do I need to get user's location information admission and write in my code?
Here's my code
#import "ViewController.h"
#interface ViewController ()<CLLocationManagerDelegate>
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
[self.locationManager startUpdatingHeading];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[self.locationManager stopUpdatingLocation];
self.Lat.text = [NSString stringWithFormat:#"%f", manager.location.coordinate.latitude];
NSLog(#"%f",manager.location.coordinate.longitude);
self.Long.text = [NSString stringWithFormat:#"%f", manager.location.coordinate.longitude];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
self.Mag_heading.text = [NSString stringWithFormat:#"%f",newHeading.magneticHeading];
self.True_heading.text = [NSString stringWithFormat:#"%f", newHeading.trueHeading];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
Once configured, the location manager must be "started"
for iOS 8, specific user level permission is required,
"when-in-use" authorization grants access to the user's location
important: be sure to include NSLocationWhenInUseUsageDescription along with its
explanation string in your Info.plist or startUpdatingLocation will not work.
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
you have to replace below code.
Use newLocation instead of manager
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.Lat.text = [NSString stringWithFormat:#"%f", newLocation.coordinate.latitude];
NSLog(#"%f",newLocation.coordinate.latitude);
self.Long.text = [NSString stringWithFormat:#"%f", newLocation.coordinate.longitude];
[self.locationManager stopUpdatingLocation];
}

iOS 8 beta - Location Manager Not Registering User Location

Since the new iOS 8 beta release, I have not been able to successfully get a user's location. Prior to the update to iOS 8 I had no issues, but now it always returns 0.000000 as the current latitude and longitude. Is this just a bug in the new release? My code is listed below:
//from the .h file
#interface MasterViewController : PFQueryTableViewController<CLLocationManagerDelegate,UITextFieldDelegate, UISearchBarDelegate, UISearchDisplayDelegate> {
}
#property (nonatomic, strong) CLLocationManager *locationManager;
//from the .m file
#synthesize locationManager = _locationManager;
- (void)viewDidLoad {
[super viewDidLoad];
[self.locationManager startUpdatingLocation];
}
- (CLLocationManager *)locationManager {
if (_locationManager != nil) {
return _locationManager;
}
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
return _locationManager;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
}
UPDATE
This question has been answered (Location Services not working in iOS 8). For anyone still struggling with this, to maintain backwards compatibility with iOS 7, I used the code below:
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) { }
As mentioned in your update/comment, iOS8 requires you to use requestAlwaysAuthorization or requestWhenInUseAuthorization as well as a new NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist
But there is something else which is wrong in your code:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
It has been deprecated in iOS6 and you should now use this new delegate method instead:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
You can then get the latest location with something like this:
CLLocation *newLocation = [locations lastObject];

How to get users current location on app launch

I want to get users current location(lat,long) on the app launch only..i dont want to use delegate method for frequent location updates..I m not using maps just need current location on launch..how should i do this??
I am using the code below but its not working.
CLLocationManager *locationManager;
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
CLLocation *location;
location = [locationManager location];
CLLocationCoordinate2D coord;
coord.longitude = location.coordinate.longitude;
coord.latitude = location.coordinate.latitude;
NSLog(#"latitude %f longitude %f",coord.latitude,coord.longitude);
Output:-
latitude 0.000000 longitude 0.000000
please help..
better you use the delegate method to get user location and in didupdateLocation method stop it
I think this url may help you
iPhone SDK: Track users location using GPS
and in
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
CLLocationCoordinate2D here = newLocation.coordinate;
NSLog(#"%f %f ", here.latitude, here.longitude);
[locationManager stopUpdatingLocation];
}
and you done with it...
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
// 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];
[locationManager stopUpdatingLocation];
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
}
Use CLLocationManagerDelegate method
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
[_locationManager stopUpdatingLocation];
NSLog(#"latitude %f longitude %f",newLocation.coordinate.latitude,,newLocation.coordinate.longitude);
}
Above method is deprecated in iOS6, use locationManager:didUpdateLocations: instead
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations
import <CoreLocation/CoreLocation.h>
delegate: CLLocationManagerDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// System alert for Allowing current location of user.
// Location manager object creation.
locationManager = [[CLLocationManager alloc] init];`enter code here`
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
// iOS >= 6.0.
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[locationManager stopUpdatingLocation];
}
// iOS < 6.0
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locationManager stopUpdatingLocation];
}

Resources