CLLocationManager "Turn On Location Services" Cancel Button press delegate - cllocationmanager

I want to know and take some action if the user has pressed cancel button in the
"Turn On Location Services" prompt. I have tried creating a breakpoint on the
(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
delegate method but this breakpoint does not get hit.
I want to know if the location services is off and the user has hit cancel so that I may not get into the same workflow again and again.

Implement the CLLocationManagerDelegate's locationManager:didChangeAuthorizationStatus: method. When your app launches, the authorization status will be kCLAuthorizationStatusNotDetermined. When the user chooses Yes or No, this method will be called with a status of kCLAuthorizationStatusAuthorized or kCLAuthorizationStatusDenied.

Related

CLLocationManager requestAlwaysAuthorization not showing alert second time

Calling [CLLocationManager requestAlwaysAuthorization] doesnt show the alert after the user selected "Dont Allow" option for the first time. Is there anyway to force the app to show the alert again when needed?
Apple won't display your alert if the user has already pressed Don't Allow.
On the other hand, you can check the authorization status and show a pop telling the user to go to settings and changing it manually.
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status == kCLAuthorizationStatusNotDetermined) {
// Show request
}
Regards

Handle Notification Permission Alert buttons clicks

Can we handle the buttons of notification's permission alert? I have attached an image of this alert. This alerts comes when we install the app for the first time on a device. If the user clicks on "Don't Allow" then the notification feature will be disabled for this app.
So, if the user clicks "Don't Allow", then I want to display a confirmation alert and inform the user that they can enable it from settings again. For this purpose I need to handle notification Alert's button (see the attached image).
If anyone knows the answer to this, please reply.
Thank you.
If push notifications were disallowed by user the delegate method :
- (void)application:(UIApplication *)app didFailToRegisterForRemoteNotificationsWithError:(NSError *)err { }
should be called. This should be the place to show your alert view.
Give it a try!
Cheers

How to stopupdatingLocation when the app goes to background (ios6)

I have instantiated locationManager in many of my view controllers. In order to save on battery, I want to stop the locationmanager from updating the location, so I was thinking of calling stopUpdatingLocation inside viewDidUnload in my VCs but viewDidUnload is being deprecated in ios6. Is there a clean way for me to stop all the updatinglocation?
Thanks!
Cuong
You can use the UIApplicationDelegate method :
- (void)applicationWillResignActive:(UIApplication *)application
Inside this method you can call stopUpdatingLocation.
stop Updating the location in
- (void)applicationDidEnterBackground:(UIApplication *)application
{
//stop updating Location
}
An earlier answer suggested calling stopUpdatingLocation from applicationWillResignActive. The better solution is to make the call from applicationDidEnterBackground instead.
I've found that calling stopUpdatingLocation from applicationWillResignActive causes the alert that asks the user for permission to use the location to disappear immediately before the user can tap anything.
This occurs because applicationWillResignActive is called when the permission dialog appears. This will call stopUpdatingLocation immediately after startUpdatingLocation, causing the dialog to immediately disappear.

Keep GPS running for a few minutes when pressing the Home button

I have an App with Region Monitoring but for better presition I call to "startUpdatingLocation" for use the GPS when the user enter into the Region specified:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
[manager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
locationManager =manager;
CLLocation *location1 = locationManager.location;
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:obj.latitude longitude:obj.longitude];
float betweenDistance=[location1 distanceFromLocation:location2];
if((betweenDistance/1000)<=0.350000){
// Fires an UIAlert
}
}
That works fine in the most of cases, for example if the user is outside the region and then press the "Home Button" and after enter into the region fires the function "didEnterRegion" and the GPS starts to work for a few minutes and show the Alert, thats the right way.
But if the App is open and then the user enter in the region fires the function "didEnterRegion" and the GPS starts, but if the user press the "Home Button" in that moment the GPS stops and the Alert never appear.
I don't want to activate the option of "Required Background modes" in the info.plist because I only want to use GPS for a few minutes after the user press de Home Button, and not for to use in a excesive mode.
Any ideas?
When your application delegate gets the message applicationWillResignActive: you can request that the OS keeps your app running by creating a request like this:
UIBackgroundTaskIdentifier taskID = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// do you cleanup in here if your background task gets terminated by the system
}];
After that, your event loop will keep running and you'll continue to receive events like normal (except your app's window isn't visible, so you obviously won't receive touch events or other window-related events).
Once you no longer need to be kept running you can release your request like this
[[UIApplication sharedApplication] endBackgroundTask:taskID];
To make it all work, just add a flag to your app delegate to let it know when you're doing something that requires more time. Then, when your delegate gets the applicationWillResignActive: message it can check that flag and, if it's set, request some background time to keep running.

How to handle "Cancel" button on Alert pop up for Location services

I am using location services in my application. When I run the application with location services ON, everything is perfect. But, if I turn the Location services OFF globally, and come back to the app. It gives me a pop up saying "Turn on Location services to allow to determine your location" with two buttons "Settings" and "Cancel".
If I tap on Settings, it takes me to Settings app (as expected). But If I tap on "Cancel" nothing happens. My app can't get the event and it freezes, waiting for something to happen. If I fire some other event after that, which uses current location, then it gets authorisation failed, and app becomes normal.
To handle this behaviour I want to detect the touch event on "Cancel" button. Is there any way to do that. Because I think this pop is handled by OS and not the SDK.
Correct me if I am wrong.
This is where to detect the touch event on "Cancel" button:
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
}
Probably, Your implementation of it, causes the freezes (And not the iOS).
Try and use this empty implementation just to make sure...

Resources