//View did Load Method- LocationManager is allocated and have included the CLLocationManagerDelegate in .h File
-ViewDidLoad{
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
self.locationManager.distanceFilter=kCLDistanceFilterNone;
[self.locationManager requestWhenInUseAuthorization];
[self.locationManager startMonitoringSignificantLocationChanges];
[self.locationManager startUpdatingLocation];
}
// Location Manager Delegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"%#", [locations lastObject]);
}
In plist you have to add 2 entries
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
Make the string of both as "Location is required to find out where you are" or anything
self.locationManager = [[CLLocationManager alloc]init];
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
self.locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];
if (authorizationStatus == kCLAuthorizationStatusAuthorized ||
authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {
NSLog(#"You are authorized");
}
self.locationManager.delegate = self;
[self.locationManager startUpdatingLocation];
Hope this helps
Related
How to fetch the location for only one time in ios app.
After getting your location, stop update location manager and also release locationManager because you don't need locationManager anymore.
[self.locationManager stopUpdatingLocation];
self.locationManager.delegate = nil;
self.locationManager = nil;
In ios 9 and above we have a method [locationManagerInstance requestLocation]. But this will take almost ten secs to call back the delegate methods since the location is latest and best.
In another way (for earlier ios9 versions), you could still use the old method [locationManagerInstance startUpdatingLocation] to get the immediate location and also you could validate the timestamp for the best accuracy.
Here is the code that I'm using for getting the location. Create a global reference for locationManager instance.
#interface ViewController ()
{
CLLocationManager *locationManager;
}
Implement these utility methods in your.m file
-(BOOL)canUseLocationManager{
if(([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedWhenInUse) || ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways)){
return YES;
}
return NO;
}
-(void) getCurrentLocation {
locationManager.delegate = self;
// ios 9 and above versions only
//[locationManager requestLocation]; // This may take more time when compare to alternate method
// ios 2 and later versions can use this method
[locationManager startUpdatingLocation];
}
-(void) stopGettingCurrentLocation {
[locationManager stopUpdatingLocation];
}
Implement location service delegates
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if([CLLocationManager authorizationStatus] != kCLAuthorizationStatusNotDetermined){
[self doneCheckingAccessStatusForLocation];
}
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
[manager stopUpdatingLocation];
locationManager.delegate = nil;
/* Clean the locationManager instance if you don't need*/
//locationManager = nil;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
[manager stopUpdatingLocation];
locationManager.delegate = nil;
CLLocation* location = [locations lastObject];
NSDate* eventDate = location.timestamp;
/* Implement your business logics here */
/* Clean the locationManager instance if you don't need*/
//locationManager = nil;
}
Final methods to create the location manager instance and initiate fetching location
-(void)checkForLocationService{
if(!locationManager){
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
}
locationManager.delegate = self;
if([CLLocationManager authorizationStatus] == kCLAuthorizationStatusNotDetermined){
[locationManager requestWhenInUseAuthorization];
}else{
[self doneCheckingAccessStatusForLocation];
}
}
-(void)doneCheckingAccessStatusForLocation{
if([self canUseLocationManager]){
[self getCurrentLocation];
}
}
call the checkForLocationService method and implement your logics on the success and failure delegate methods.
[self checkForLocationService];
here is my code :
if (!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
}
if([CLLocationManager locationServicesEnabled]){
[self findCurrentLocation];
}
-(void)findCurrentLocation {
if ([self.locationManager respondsToSelector:#selector(requestAlwaysAuthorization)]) {
[self.locationManager performSelector:#selector(requestAlwaysAuthorization)];
}
[self.locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.isLocationEnabled=YES;
self.currentLocation = newLocation;
if (currentLocation!= nil) {
latitudeValue = [NSString stringWithFormat:#"%.8f",currentLocation.coordinate.latitude];
longitudeValue = [NSString stringWithFormat:#"%.8f",currentLocation.coordinate.longitude];
}
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
if([error code] == kCLErrorDenied)
{
[locationManager stopUpdatingLocation];
}
self.isLocationEnabled=NO;
}
i am not getting any errors, but didUpdateToLocation is never called, it is going into didFailWithError ()
I am getting null values for latitude and longtitude. Please let me know what can be done to get values.
from iOS8 and above:
Add a key to your Info.plist and request authorization from the location manager asking it to start.
NSLocationWhenInUseUsageDescription
NSLocationAlwaysUsageDescription
You need to request authorization for the corresponding location method.
[self.locationManager requestWhenInUseAuthorization]
[self.locationManager requestAlwaysAuthorization]
Please Read this Documentation: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
Can you please review following code, it did tries locationManager with delegates, and tries to return current location of device.
But always returns 0 (allowed location permission)
.m
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
[self.locationManager startUpdatingLocation];
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusAuthorizedAlways) {
// TODO: Handle status authorized always
} else if (status == kCLAuthorizationStatusNotDetermined) {
// TODO: Handle status not determined
} else {
// TODO: Handle other
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
//NSLog(#"newLocation: %#", newLocation);
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//NSLog(#"Error: %#",[error description]);
}
info.plist
<key>NSLocationAlwaysUsageDescription</key>
<string>We would like to use your location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>We would like to use your location.</string>
Please try to call startUpdatingLocation after requestAlwaysAuthorization. and use below didUpdateLocations delegate methods.
if(IS_OS_8_OR_LATER) {
[self.locationManager requestAlwaysAuthorization];
}
[self.locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
_currentLocation = [locations objectAtIndex:0];
//do your stuff with the location
}
For Simulator Testing - Select Simulator and goto Debug ->Location -> Apple or City Run
For Device Testing Please check with map if you are getting current location or not . Sometime device not getting current location due to inside of house.
Please don't mark it as duplicate because I took help from others posted answer in stack overflow. But still I faced some issues. Location manager delegate not get called even in real device also. And also it's not asking for the permission.
Below is my code.
- (IBAction)getUserLocationAction:(id)sender
{
if([CLLocationManager locationServicesEnabled])
{
if(!locationManager)
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
if ([locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[locationManager requestWhenInUseAuthorization];
}
[locationManager startUpdatingLocation];
//[locationManager startMonitoringSignificantLocationChanges];
}
else
{
NSLog(#"Location service is not enabled");
}
}
#pragma mark - Location Manager Delegate-
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"%#", locations);
userLocation = [locations lastObject];
NSLog(#"User Current Locatiion\nLat: %+.6f\nLong: %+.6f", [userLocation coordinate].latitude, [userLocation coordinate].longitude);
[locationManager stopUpdatingLocation];
//[locationManager stopMonitoringSignificantLocationChanges];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"Error: %#", [error localizedDescription]);
}
I also add the below two key in my .plist file
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app want to use your location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app want to use your location</string>
I have also added the frameworks
I am not sure where I am doing wrong. Please help me.
UPDATE
Firstly I think its an issue with my code but after some research it's working as expected in iOS7. The code is remain same, this issue occur only in iOS8. Please let me know what changes needs to be done for working in iOS8 also.
You need to do some more checking:
- (IBAction)getUserLocationAction:(id)sender
{
if([CLLocationManager locationServicesEnabled]) {
if(!self.locationManager) {
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager setDelegate:self];
[self.locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
}
CLAuthorizationStatus authStatus = [CLLocationManager authorizationStatus];
if (authStatus == kCLAuthorizationStatusNotDetermined) {
// Check for iOS 8 method
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
else {
[self.locationManager startUpdatingLocation];
}
}
else if(authStatus == kCLAuthorizationStatusAuthorizedAlways || authStatus == kCLAuthorizationStatusAuthorizedWhenInUse || authStatus == kCLAuthorizationStatusAuthorized) {
[self.locationManager startUpdatingLocation];
}
else if(authStatus == kCLAuthorizationStatusDenied){
NSLog(#"User did not allow location tracking.");
// present some dialog that you want the location.
}
else {
// kCLAuthorizationStatusRestricted
// restriction on the device do not allow location tracking.
}
}
else {
NSLog(#"Location service is not enabled");
}
}
Also you need to handle the use callback:
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
if (status != kCLAuthorizationStatusRestricted && status !=kCLAuthorizationStatusDenied) {
[self.locationManager startUpdatingLocation];
}
}
Check your Location Access settings..so go to Setting-->Privacy-->Location Services-->Your app
my program is working fine in iOS7 but not iOS8..
locationManager is object of CLLocationManager here.
In starting this code is working fine in iOS7,i can not understand why this happen to me.
- (void)getCurrentLocation{
locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
if (status == kCLAuthorizationStatusAuthorized) {
NSUserDefaults *notify = [NSUserDefaults standardUserDefaults];
[notify setObject:#"GPS" forKey:#"Notification"];
[notify synchronize];
appDelegate = (JobDiagnosisAppDelegate *)[[UIApplication sharedApplication] delegate];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
[appDelegate showIndicator];
[notify setObject:nil forKey:#"Notification"];
[notify synchronize];
}
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"didFailWithError: %#", error);
[appDelegate.objActivityAlertView close];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:FALSE];
appDelegate = (JobDiagnosisAppDelegate *)[[UIApplication sharedApplication] delegate];
[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:TRUE];
[appDelegate showIndicator];
[self callWebserviceLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
NSLog(#"Resolving the Address");
[geocoder reverseGeocodeLocation: locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
//Get address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to address
NSString *locatedaddress =[placemark valueForKey:#"administrativeArea"] ;
//Print the location in the console
NSLog(#"Currently address is: %#",locatedaddress);
NSUserDefaults *notifyLocation = [NSUserDefaults standardUserDefaults];
[notifyLocation setObject:locatedaddress forKey:#"notifyLocation"];
[notifyLocation synchronize];
locationManager.delegate = nil;
[self callWebserviceUnRegistered];
}];
[locationManager stopUpdatingLocation];
}
please help to solve this location problem.I am new in location services.
thanks in advance!!
You Need to implement
[locationManager requestWhenInUseAuthorization];or
[locationManager requestAlwaysAuthorization];
If you do not make either of two request , iOS will ignore startUpdateLocation request.
Also,
Include NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist depending upon which permission you are asking for. This string will be diaplayed by iOS to user so the user can get excat idea why does our app needs permission.
So change your methood to
- (void)getCurrentLocation{
locationManager = [[CLLocationManager alloc] init];
geocoder = [[CLGeocoder alloc] init];
locationManager.delegate = self;
[locationManager requestWhenInUseAuthorization];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
And aslo add required key in info.plist.
Hope this helps.
First, don't forget, with IOS 8 you need to add NSLocationWhenInUseUsageDescription property to the info.plist file in order to prompt the alert to get the user's permission and use[locationManager requestWhenInUseAuthorization] plus [locationManager requestAlwaysAuthorization];
_locationManager = [CLLocationManager new];
if(SYSTEM_IS_OS_8_OR_LATER) {
[_locationManager requestWhenInUseAuthorization];
[_locationManager requestAlwaysAuthorization];
}
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyBest;
_locationManager startUpdatingLocation];
and in your didUpdateLocations
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
_currentLocation = [locations objectAtIndex:0];
//do your stuff with the location
}