continously check for location services enabled in ios - iphone

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???

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

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];
}

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++;
}
}

Related

GPS Location Permission box not appering

I am trying to make a gps location app in which it gets the users latitude and longitude.I want to request the location when user starts the app .
if([self.locationManager
respondsToSelector:#selector(requestWhenInUseAuthorization)])
{
[self.locationManager requestWhenInUseAuthorization];
}
else
{
[self.locationManager startUpdatingLocation];
}
But its not helping i also try
if([CLLocationManager locationServicesEnabled]){
NSLog(#"Location Services Enabled");
if([CLLocationManager authorizationStatus]==kCLAuthorizationStatusDenied){
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"App Permission Denied"
message:#"To re-enable, please go to Settings and turn on Location Service for this app."
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
}
}
Its not helping also.
You would need to add a key for location request
NSLocationWhenInUseUsageDescription in your plist file
and NSLocationAlwaysUsageDescription key if you want location in background also.

locationManager didFailWithError Repeatedly Called

I am showing alertViews when Location manager unable to find current location. I did it like this
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
[manager stopUpdatingLocation];
switch([error code])
{
case kCLErrorNetwork: // general, network-related error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Please check your network connection or that you are not in airplane mode" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
break;
case kCLErrorDenied:{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"User has denied to use current Location " delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Error" message:#"Unknown network error" delegate:self cancelButtonTitle:#"Ok" otherButtonTitles:nil, nil];
[alert show];
[alert release];
}
break;
}
}
My problem is locationManager didFailWithError method get called repeatedly. So my alertviews repeatedly display.
How can I solve this issue?
Apple documentation states:
If the location service is unable to retrieve a location right away,
it reports a kCLErrorLocationUnknown error and keeps trying. In such a
situation, you can simply ignore the error and wait for a new event.
If the user denies your application’s use of the location service,
this method reports a kCLErrorDenied error. Upon receiving such an
error, you should stop the location service.
So you might skip some cases where you stop updating and show the alert, especially the default case.
A good start for error messages that can be triggered by an ongoing background process - like the location manager - is to store the reference to the UIAlertView. When the delegate receives alertView:clickedButtonAtIndex:, reset this reference to nil. Before displaying new errors, check if one is already visible.
Even better, don't display modal error messages from background processes. Just walking around, you'll occasionally lose reception, and it's tedious to have to dismiss an error when that happens. If your app has a configuration or help screen, include a space there for the most recent error message from the Location Manager (or Game Center, or iCloud, or anything else that loses connectivity occasionally.)

Stuck on CLLocation

This is more of a theoretical question than code:
What i did till now :
My app is heavily dependent upon location tracking.
I posted a question about it around 5 hours back without a suitable answer and it is now pretty old to put a new question there .i will closing it after i get an answer for this question.
What i want to do is to have a button that user presses.it makes location manager to start updating locations and then for every 5 feet moved . i want to make iPhone play random sounds.
i implemented correct code given below and now i tested on my device moving from one place to here in my house. through trial and errors i got to know that first few locations are iPhone trying to get an accurate results and are generally to be avoided so i made it a condition to process location which come after more than 2 seconds to previous.
Now my Real ques :
I have my device on wifi.(don't have gps and can't have) so after few initial useless location updates . my alerts stop showing.. i was like : why isn't working . i set it to accuracy best. did filtering to 1.5 meters .. i was moving like 15 feets in the house and no update ?..now i think it is because it is taking locations through wifi and hence it is founding no new locations even if i move 40 feeds around that wifi.? am i correct?
If that is true .. can anyone tell will this work correctly in gps .. for U.S. users..where they have lots of towers.. so that my app will correctly update that new location is at least 5 meters away from its previous location..
Sorry for the long question.. There are too many doubts for me..
-(id) init
{
if(self = [super init])
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
// locationManager.distanceFilter = 1.5;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
return self;
}
return nil;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"New Lcoation"
message:nil
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
self.currentLocation = newLocation;
BOOL newLocationIsValid = [self isValidLocation:newLocation withOldLocation:oldLocation];
if(newLocationIsValid && oldLocation)
{
int distance = [newLocation distanceFromLocation:oldLocation];
if(distance >2 && distance <10)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Distance"
message:[NSString stringWithFormat:#"%i meters",distance]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
[fileURl release];
}
}
}
-(BOOL)isValidLocation:(CLLocation*)newLocation withOldLocation:(CLLocation*)oldLocation
{
if(newLocation.horizontalAccuracy < 0 )
{
return NO;
}
NSTimeInterval secondsSinceLastPoint = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
if(secondsSinceLastPoint < 0)
{
return NO;
}
if(secondsSinceLastPoint < 2)
{
int distance = [newLocation distanceFromLocation:oldLocation];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Distance"
message:[NSString stringWithFormat:#"%i meters",distance]
delegate:nil
cancelButtonTitle:#"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
return NO;
}
return YES;
}
yes, doesn't work because you use wifi for locating. The Location-Service just know where your wifi is, not your position relative to your wifi (that would be nice).
so for your service you must use gps. and even then, the accuracy is not garanteed.
see http://www.kowoma.de/en/gps/accuracy.htm
When you are not using the real GPS signal the location is retrieved via triangulation, which is in no way near accuracy you want. Even if there are many WiFi or cell towers the accuracy is still a couple of meter, more like 100 then 10.
The accuracy you want can only be achieved with GPS, which is some cases will only have an accuracy of 100 meters or so. It all depends on the GPS signal quality.

Location Manager update frequency, iphone

I have a CLLocation manager called "myLocation".
myLocation = [[CLLocationManager alloc] init];
myLocation.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
myLocation.distanceFilter = 10 ;
myLocation.delegate=self;
locationEnabledBool = [CLLocationManager locationServicesEnabled];
if (locationEnabledBool ==NO || ( [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied)) {
// LocationText.text = #"Location Service Disabled ";
UIAlertView *locationAlert = [[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];
[locationAlert show];
[locationAlert release];
}
[myLocation startUpdatingLocation];
and location update function is
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(#"old location is %f, %f ", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(#"new location is %f,%f",newLocation.coordinate.latitude, newLocation.coordinate.longitude );
}
Is there a way to find frequency of location manager update, and If it can be increased or decreased?
Your location update starts only when you call the method [locationManager startUpdatingLocation].
You can control the frequency of the update using an NSTimer. Call the startUpdatingLocation method at regular intervals whenever you need a location update and then immediately call the stopUpdatingLocation method. The next time you will get a location update only at the interval you have set in the NSTimer.
For detecting even the slightest of movements, you need to set
myLocation.distanceFilter = kCLDistanceFilterNone ;
But, please keep in mind,letting location manager to generate updates for even the slightest of movements can end up in lot of battery usage.

UIAlertView Pops Up Three Times per Call Instead of Just Once

I am getting odd behavior from an NSAlert in two different parts of my program. The behavior is:
Alert appears and then spontaneously disappears.
Alert reappears and then remains until dismissed by user i.e. normal behavior.
Alert reappears again.
This behavior only occurs the first time the method that displays the alert is called. After that first time, it behaves normally.
Here is the code for the one of the parts in which the behavior occurs:
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
Or if you prefer, with a bit more context:
- (IBAction)locateMe {
NSLog(#"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation * )oldLocation {
if (newLocation.horizontalAccuracy >= 0) {
CLLocation *airportLocation = [[[CLLocation alloc] initWithLatitude:51.500148 longitude:-0.204669] autorelease];
CLLocationDistance delta = [airportLocation getDistanceFrom: newLocation];
long miles = (delta * 0.000621371) + 0.5; //metres to rounded mile
if (miles < 3) {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
} else {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are not in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locationAlert release];
[locMan stopUpdatingLocation];
}
}
}
- (void) locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"Error." message:error.code delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
[locMan release];
locMan = nil;
}
Any ideas? Thanks.
Edit---------
The other place this happens is:
- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
NSString * errorString = [NSString stringWithFormat:#"Unable to download feed from web site (Error code %i )", [parseError code]];
NSLog(#"error parsing XML: %#", errorString);
UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:#"Error loading content" message:errorString delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[errorAlert show];
}
For context the first case is in the AppDelegate and the second in the view controller for the 1st tab view. The second problem occurs every time the xml is reloaded when there is no internet connection. The first one only occurs the first time the function is called.
Edit-----
If I move the alert it works. Unfortunatly this is not where I want it!
- (IBAction)locateMe {
UIAlertView * locationAlert = [[UIAlertView alloc] initWithTitle:#"You are in the right place." message:nil delegate:self cancelButtonTitle:#"OK" otherButtonTitles:nil];
[locationAlert show];
/*
NSLog(#"About to check location");
locMan = [[CLLocationManager alloc] init];
locMan.delegate = self;
locMan.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
locMan.distanceFilter = 1609; //1 mile
[locMan startUpdatingLocation];*/
}
Update:
I set some NSLog entries and discovered that despite the addition of [locMan stopUpdatingLocation] the didUpdateToLocation function was running multiple times.
I guess the spontaneous disappearance happens because the alert view is called again and the programme clears the first instance to make way for the second automatically.
Any ideas as to why [locMan stopUpdatingLocation] doesn't work would be appreciated but in the mean time I just moved the declaration of the locationAlert out of the function (so it is global), set it in the initial locate me function and use the following the first time it is called:
[locationAlert show];
locationAlert = nil;
That way it works perfectly.
You're not turning off your location manager when you first show the alert. As the location is refined by the device (ie, the accuracy is increased), your callback will be (potentially) called multiple times. You should use [locMan stopUpdatingLocation] after your alert display.
I set some NSLog entries and discovered that despite the addition of [locMan stopUpdatingLocation] the didUpdateToLocation function was running multiple times.
I guess the spontaneous disappearance happens because the alert view is called again and the programme clears the first instance to make way for the second automatically.
Any ideas as to why [locMan stopUpdatingLocation] doesn't work would be appreciated but in the mean time I just moved the declaration of the locationAlert out of the function (so it is global), set it in the initial locate me function and use the following the first time it is called:
[locationAlert show];
locationAlert = nil;
That way it works perfectly.
I think the NSAlert disappearing on its own is the key to solving this.
It's simple to explain why an alert displays unexpectedly i.e. it's just been called unexpectedly. However, it's not so common to programmatically dismiss an alert. Whatever is causing it to disappear is most likely triggering the display again.
To debug I suggest:
(1) Looking in your code for the NSAlert – dismissWithClickedButtonIndex:animated: method and see if somehow you're actually dismissing the alert programmatically.
(2) I believe (someone double-check me on this) that an alert view is added as a subview to whichever base view is currently on screen. It might be that the base view is disappearing for some reason and taking the alert view with it. If the view disappears and then reappears rapidly enough, it might not be obvious when the alert is frontmost. (Edit: see Ed Marty's comment below.)
(3) Since this happens in two separate pieces of the app, compare both to find a common element or structure. That common element might be the cause.
An odd problem.
Edit01: Updated for additional info
If locMan isan instance variable, it should be defined as a property and you should access it every time withself.locMan By accessing it directly, you lose your automatic retention management.
I encountered the same exact issue with the alert dialog appearing momentarily, reappearing, and finally appearing again after being dismissed. I was making a string comparison before deciding to show the alert view:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:#"OK"]) {
NSLog(#"(Settings)Registration Successful");
statusField.text = #"Registration successful!";
[settingsActivity stopAnimating];
}
else {
NSLog(#"(Settings)Registration Failure");
[settingsActivity stopAnimating];
UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:#"Registration Error!" message:#"Please check your email address and try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[regFail show];
}}
To correct this behavior I simply verified the returned string rather than just showing the alert:
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string {
if([string isEqualToString:#"OK"]) {
NSLog(#"(Settings)Registration Successful");
statusField.text = #"Registration successful!";
[settingsActivity stopAnimating];
}
else if([string isEqualToString:#"Error"]) {
NSLog(#"(Settings)Registration Failure");
[settingsActivity stopAnimating];
UIAlertView * regFail = [[[UIAlertView alloc] initWithTitle:#"Registration Error!" message:#"Please check your email address and try again." delegate:self cancelButtonTitle:#"OK" otherButtonTitles: nil] autorelease];
[regFail show];
}
I also got the Same problem while working on Location Manager. Here i checked with Nslog but it is executing multiple times, finally i fount that i am creating multiple objects and using Sharedinstance for same ViewController that contains Location Manger but i am not releasing the object, so at perticular location how many objects if we create that many times the location detects.So while working on LocationManger check handling objects thoroughly to reduce these type of problems.