khushalbokadey
khushalbokadey

Reputation: 1152

continously check for location services enabled in ios

I am trying to make an app in which the view will check whether the location services are enabled or not . If it is not enabled then it will prompt one with a pop up but still it will keep on searching for location but not prompt. As soon as the location services are enabled it will continue its process.

How to do that???

Upvotes: 1

Views: 1642

Answers (3)

DLende
DLende

Reputation: 5242

You cannot continue getting the location if location service are disabled.

If you want to continue searching for location be sure that the service is enable by checking

[CLLocationManager locationServicesEnabled]

If enabled, start updating the location

[locationManager startUpdatingLocation];

Then in

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    [locationManager stopUpdatingLocation]; // This will stop to check the location
}

remove this code to still check the location [locationManager stopUpdatingLocation];, but this is not the best approach, be sure to read the apple documentation for the policy of getting the location

Upvotes: 1

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

Add in .h file int counter;

In your view's viewDidLoad method add this as it will check for every second u can increase counter:

[NSTimer scheduledTimerWithTimeInterval:1.0     
                             target:self
                           selector:@selector(checkLocationMangerEnabled:)     
                           userInfo:nil     
                            repeats:YES];

 counter = 0;

Now selector would be:

-(void)checkLocationMangerEnabled:(id)sender
{
   if([CLLocationManager locationServicesEnabled])
   {
      //here location is enabled
   }
   else
   { //Not enabled
     if(counter == 60) // alert will showed in every 1 min u can increase or decrese
     {
       UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                    message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                   delegate:nil 
                                          cancelButtonTitle:@"OK" 
                                          otherButtonTitles:nil];
      [alert show];
      [alert release];
    }
    counter++;
   }
}

Upvotes: 0

Mansi Panchal
Mansi Panchal

Reputation: 2357

You can check location services availability through this code :

MKUserLocation *userLocation = map.userLocation;
    BOOL locationAllowed = [CLLocationManager locationServicesEnabled];
    BOOL locationAvailable = userLocation.location!=nil;

    if (locationAllowed==NO) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Location Service Disabled" 
                                                        message:@"To re-enable, please go to Settings and turn on Location Service for this app." 
                                                       delegate:nil 
                                              cancelButtonTitle:@"OK" 
                                              otherButtonTitles:nil];
        [alert show];
        [alert release];
    } else {
        if (locationAvailable==NO) 
            [self.map.userLocation addObserver:self forKeyPath:@"location" options:(NSKeyValueObservingOptionNew|NSKeyValueObservingOptionOld) context:nil];
    }

Upvotes: 0

Related Questions