Problem: It seems I can't stop Core Location from sending updates to my app / tracking.
What am I doing: In my viewWillAppear I call self.locationManager and pass it to a method to show user's location on the map (an instance of MKMapView). The getter is overridden to check for availability of the serive, to see whether its authorized. If so, it allocs/inits and startMonitoringSignificantLocationChanges and returns.
In viewDidDisappear, I call [self.locationManager stopUpdatingLocation]. But still I can see the location icon in the status bar. Even when I terminate the app by double tapping the home button and closing it, the icon is still there... even after extended amount of time (10+ minutes). When I go to the Settings App, it tells me that my app is still using location services (purple icon).
Question: What am I missing here? Why location update doesn't stop?
Thanks in advance.
The opposite of startMonitoringSignificantLocationChanges is not stopUpdatingLocation, it is stopMonitoringSignificantLocationChanges.
You probably want to replace startMonitoringSignificantLocationChanges with startUpdatingLocation for the sake of more regular updates, unless you have a specific reason for monitoring only for significant location changes.
Check out the CLLocation documentation for further detail.
I too just experienced the same problem as Canopus. It appears that even if stopUpdatingLocation is called the navigation pointer still resides on the status bar if I have showUserLocation enabled. Perhaps this is a bug? It may be as I am running and testing with iOS 4.2.1 and this issue may have been fixed in a later SDK.
I would think that if stopUserLocation is called it would also stop showing the user location since the view I am using it in has already disappeared and is subsequently deallocated.
It appears that you must set showUserLocation to NO before stopping user location updates.
Anyway, in my viewDidLoad method I have the following code:
self.mapView.showsUserLocation = YES;
More code...
- (void)viewWillDisappear:(BOOL)animated
{
if (locationManager)
{
mapView.showsUserLocation = NO;
[locationManager stopUpdatingLocation];
}
[super viewWillDisappear:animated];
}
- (void)dealloc
{
if (locationManager)
{
[locationManager release];
locationManager = nil;
}
(other code)
}
Swift:
Your map AND location manager both need to be stopped:
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
locationManager.stopMonitoringSignificantLocationChanges()
locationManager.stopUpdatingLocation()
mapView.showsUserLocation = false
}
You can debug/check location services usage right there in Xcode, in the debug navigator under Energy Impact.
I solved this setting nil to locationManager property after delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// Your code getting coordinates
//Here set nil
locationManager = nil;
}
My app requests "always" auth. Only a certain feature within the app requires that. If the user turns that feature off then on app close we can stop location updates (with the goal of saving battery and removing the location icon from the status bar). I too thought stopping was not working because after app close the location icon in the status bar was still there even though my app was the only app running on my phone with location access and "on app close" I just told it to stop updating locations.
For me the solution was to be a bit more patient and then I realized that it takes iOS about 10-15 seconds to turn off location hardware and update the status bar. I didn't have to nil out my location manager or anything special, just stopped updates on app close, waited 15 seconds, and observed iOS remove the location icon from the status bar. Hope this helps somebody out there!
I was working with CLLocationManager in Swift and I think is relevant to the either Swift or Objective-C but, I just created a boolean which I update to true whenever I have received the location update. Since in my case I just need it once on launch.. Example,
// my boolean to stop location updates
var alreadyUpdatedLocation = Bool()
Also, in my case I have created a helper function that whenever I get the data/location of the user, I just call my own stopUpdatingLocation like this,
// helper function to stop updates
func stopUpdationgLocation() {
// since I received my data within a block, I don't want to just return whenever it wants to :)
dispatch_async(dispatch_get_main_queue()) {
// the building stop updating location function call
self.locationManager.stopUpdatingLocation()
// my own trick to avoid keep getting updates
self.alreadyUpdatedLocation = true
}
}
Then, where ever you use the location updates that you have received, you could do this...
// avoiding multiple calls after I have received user location
if(self.alreadyUpdatedLocation) {
return
}
I hope it helps!
try this..
if ([CLLocationManager significantLocationChangeMonitoringAvailable])
{
[self.locationManager startMonitoringSignificantLocationChanges];
}
[[self locationManager] stopUpdatingLocation];
If you are using the GoogleMaps SDK for iOS, I found that if you call
locationManager.stopUpdatingLocation()
and still have
mapView.isMyLocationEnabled = true
then the gps icon remains.
What I chose to do is initially show the user's location on the map and then turn it off after 8 seconds. This worked for me using the GoogleMaps SDK. I added this in ViewDidLoad:
DispatchQueue.main.asyncAfter(deadline: .now() + 8.0, execute: {
self.locationManager.stopUpdatingLocation()
self.mapView.isMyLocationEnabled = false
})
You could put the same code in viewWillDisappear if you prefer to not have the gps icon in the status bar when you segue to another view.
Problem:
In my case I use both startMonitoringSignificantLocationChanges and startUpdatingLocation.
Even after stopping the location through locationManager.stopMonitoringSignificantLocationChanges() &
locationManager.stopUpdatingLocation().
My location is getting called continuously.
Solution:
1. Check whether you have invalidated the timers.
2. Initialize locationManager.delegate = nil.
These will surely solve your problem.
Related
I'm developing an application that needs to constantly update user's location even in the background. Using CLLocationManager, is it possible to automatically call locationManager:didUpdateLocations: delegate method after the use's location changes.
You are not supposed to manually call the locationManager:didUpdateLocations: delegate of CLLocationManager.
locationManager:didUpdateLocations: is inherently designed to provide delayed GPS updates to save battery. So if you need updates immediately, implement the locationManager:didUpdateToLocation:fromLocation: instead of
locationManager:didUpdateLocations:.
All you need to do is create an instance of CLLocationManager object and call it's startUpdatingLocation method.
self.locationManager = [[CLLocationManager alloc] init];
[self.locationManager startUpdatingLocation]
After this, locationManager:didUpdateToLocation:fromLocation: will be called immediately when the user's location changes.
Also, as you need location the background too, do not forget to add the relevant capabilities in the plist. Also, for iOS9+, do not forget the below snippet
if (SystemVersionGreaterThanOrEqualTo(#"9.0"))
{
self.locationManager.allowsBackgroundLocationUpdates = YES;
}
Note: if you implement locationManager:didUpdateLocations:, locationManager:didUpdateToLocation:fromLocation: will not be called. So do not implement both of them, only the one you need.
You can use startUpdatingLocation() method of CLLocationManager class as per following
[locationManager startUpdatingLocation];
When ever instance of your viewController active in memory this method will be called but in background case after some time OS (iOS) removed app from background when other application not getting efficient memory space for the execution.
For background execution you can put your code in background task block as per following
UIBackgroundTaskIdentifier taskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^(void) {
// Uh-oh - we took too long. Stop task.
}];
// Perform task here
// Your code
if (taskId != UIBackgroundTaskInvalid) {
[[UIApplication sharedApplication] endBackgroundTask:taskId];
}
Hope this will helpful to you:)
You don't need to call
[LocationManager startUpdating];
Every time when location is updated, There is a simple solution for this provided by Location Manager its self.
You will get desired functionality with
self.locationManager.distanceFilter = 10.0f;
//Location manager will update you when location is changed to 10 meters.
I'm first using the CoreLocation framework. I have a table and by button click a new location should be added and the distance to all entries in the table should be shown and updated all the time. That is why I have a BOOL saveNewLocation which is set to Yes when the button is clicked. Because the updates need to happen still all the time in the background, but when the button is clicked only a new entry is added.
At the moment I have this in my viewDidLoad:
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
// Check for iOS 8. Without this guard the code will crash with "unknown selector" on iOS 7.
if ([self.locationManager respondsToSelector:#selector(requestWhenInUseAuthorization)]) {
[self.locationManager requestWhenInUseAuthorization];
}
[self.locationManager startUpdatingLocation];
And this is my delegate method:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.currentLocation = newLocation;
if(self.saveNewLocation){
[PointOfInterest addPointOfInterest:newLocation withAddress:#"" andNotes:#"" inManagedObjectContext:self.cdh.context];
self.saveNewLocation = NO;
}
[self updateAllDistances];
}
And this my button:
- (IBAction)addLocationClicked:(id)sender {
self.saveNewLocation = YES;
}
But the problem at the moment is that when you click this button, there is sometimes a big lag and nothing happens. Sometimes immediately a new location is added. How can I avoid this lag and instantly add a new location by click?
The time interval between update calls to the location manager delegate is variable, so the behavior you're experiencing is expected.
CLLocationManager has a property called location which returns the last known location of the user (or nil if you've never used the Location Manager in the app).
Instead of waiting for the LocationManager to update, grab the last known location of the user instead:
- (IBAction)addLocationClicked:(id)sender {
CLLocation *location = self.locationManager.location;
if (location && [NSDate timeIntervalSinceReferenceDate] - location.timeStamp.timeIntervalSinceReferenceDate < 60 * 10){
//Do something with the location if the location manager returns a location within the last 10 minutes
} else {
self.saveNewLocation = YES;
}
}
If the app has never asked for it's location, you may get nil, in which case you'll have to wait for the locationManager to update. But otherwise, you can just grab the last known location. You can also check whether the location was update recently by checking the timeStamp on the location object.
You also may want to set a state flag indicating that the app should wait for a location update when the location manager is first used. When you first start up the LocationManager, you can't really know how up-to-date that location is. But once the manager begins updating the delegate, you can be reasonably certain the location manager holds a fairly up-to-date location.
I am trying to fetch user locations in foreground & background. I have to call api after I got a locaion update. To work in background I want to use Deferred method. I followed the same process as described in Apple WWDC. I am checking app on iPhone 5 (iOS 7). It is working fine when I am in foreground but did not give me update after I send the app into background. Below is the code which I am using to get location in background.
#import "AppDelegate.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.locationArray = [[NSMutableArray alloc] init];
self.locationErrorArray = [[NSMutableArray alloc] init];
self.manager_loc = [[CLLocationManager alloc] init];
self.manager_loc.activityType = CLActivityTypeFitness;
self.manager_loc.delegate = self;
[self.manager_loc setDesiredAccuracy:kCLLocationAccuracyBest];
[self.manager_loc startUpdatingLocation];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
#pragma mark - Location Manager Delgate
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"update failed");
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
[self.locationArray addObject:locations];
NSLog(#"udate locations %f %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude);
if (!self.deferredStatus)
{
self.deferredStatus = YES;
[self.manager_loc allowDeferredLocationUpdatesUntilTraveled:100 timeout:30];
}
[self.manager_loc stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFinishDeferredUpdatesWithError:(NSError *)error
{if (manager.location != nil)
{ [self.locationArray addObject:manager.location];
}
if (error != nil)
{
[self.locationErrorArray addObject:error.description];
}
self.deferredStatus = NO;
NSLog(#"deffered success %f %f", manager.location.coordinate.latitude, manager.location.coordinate.longitude);
}
#end
If I do not stop the location update in didUpdateToLocations Delegate then the location arrow (on status bar) do not go. In that case it gives me locations contionusly. I want location update after a particular time or particualar distance travelled, so that I can hit server with the user locations. Please help me on this.
Use LocationManger distanceFilter Property for update location at particualar distance travelled.
self.manager_loc.distanceFilter= 100;// In meters
If you want location updated in Backggriound then register your for background updates. Youca can do it in plist.
Set location manager to :
if ([self.locationManager respondsToSelector:#selector(pausesLocationUpdatesAutomatically)]) {
self.locationManager.pausesLocationUpdatesAutomatically = NO;
}
Then If you want to location updated after some time or distance then use:
- (void)allowDeferredLocationUpdatesUntilTraveled:(CLLocationDistance)distance
timeout:(NSTimeInterval)timeout // No guaranty it will work exactly or not
If you want location updated based on distance the you can use
Desired accuracty and distanceFilter property.
self.locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;// Use other accurarcy as your need
self.locationManager.distanceFilter = 3000; //100, 200, etc
If you set activity type to CLActivityTypeFitness all above setting will overrided, And location manager updated according to this activity, which is as per my knowledge will eat Battery.
While using CLLocation Manager one thing you should accept it will not give all updartes 100% accurate.
See my answer for this post: StartUpdateLocations in Background, didUpdatingToLocation only called 10-20 times
If you need location updates in the background under iOS 7, you must call startUpdatingLocation while your App is in the foreground. You can no longer do this while your App is in the background, so you can no longer register for location updates only when you need them and while you need them. You are forced to register for them for the whole time your App is running (in the foreground and the background) and so you’re forced to waste a lot of energy.
You can reduce the battery usage a little bit by setting the accuracy to kCLLocationAccuracyThreeKilometers when you do not need the location updates and set them to kCLLocationAccuracyBest only when you need the updates. But this will nevertheless drain the battery faster than expected.
Please write a bug report to Apple and ask for the „old" behavior of iOS 4,5 and 6, where you could call „startUpdatingLocation“ in the background as well to get location updates in the background. If Apple gets enough requests to change this behavior back to the way it was implemented in iOS 5/6, the more likely it is that Apple will change this back.
The currents situation is really bad. Bad for developers, which are forced to waste energy, or to abandon their Apps, bad for the user, whose device needs to be plugged to a power source much earlier, or who can no longer use certain Apps.
I have a class that launches an instance of CLLocationManager. The intent is to use it to get a decent, one-time fix on app launch and then stop location services for the duration (or until some other condition is met which requires a new fix...but that part isn't yet written).
For some reason, even though I call [stopUpdatingLocation], my app still seems to be keeping location services active indefinitely, whether in the background or not. My delegate doesn't receive updates anymore, as expected, but the arrow stays on the status bar. I turned off location services for all other apps to verify that mine was the culprit, and then killed my app manually (which immediately dismissed the arrow).
My code is based on what you find in the Apple docs, with some things added for my purposes. I've read through all the pertinent Apple docs and just can't figure out what I'm doing wrong. All the other answers on the subject address people who are using MKMapView and forget to set "showUserLocation" to NO...I'm not using an MKMapView at all, so that's not my issue. What the heck. Why won't it die?
(_locationManager is, of course my instance of CLLocationManager).
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:
(NSArray*)locations
{
CLLocation* lastLocation = [locations lastObject];
// Ignore old (cached) location
NSDate *date = lastLocation.timestamp;
NSTimeInterval howRecent = [date timeIntervalSinceNow];
if (abs(howRecent) > 30.0)
{
DLog(#"Ignoring cached fix");
return;
}
// Wait for better fix if we have GPS
if (lastLocation.horizontalAccuracy > 60 && _GPSEnabled)
{
DLog(#"Ignoring inaccurate fix on GPS-enabled device");
return;
}
// Accept mediocre fix if we don't have GPS
if (lastLocation.horizontalAccuracy < 300 && _GPSEnabled == NO)
{
_hasPos = YES;
_lastPos = lastLocation.coordinate;
DLog(#"Best fix we're likely to get without GPS: %f %f", _lastPos.latitude,
_lastPos.longitude);
[self stopTracking];
return;
}
// If we have GPS, accept a fix with <60m accuracy
_hasPos = YES;
_lastPos = lastLocation.coordinate;
DLog(#"Geolocator has good fix: %f %f", _lastPos.latitude, _lastPos.longitude);
[self stopTracking];
}
-(void)stopTracking
{
DLog(#"Stopped updating locations");
[_locationManager stopUpdatingLocation];
_locationManager.delegate = nil;
}
Finally found the answer in another question...reprinting it here because it took me a while to stumble on it.
I had to call this:
[_locationManager stopMonitoringSignificantLocationChanges];
Even though I never called startMonitoringSignificantLocationChanges in the first place, seems I had to "un-call" it...strange that it works this way, but as soon as I added that line, location services shut down promptly. Hope this helps someone else.
I am having difficulties getting this to work for when the app is not running. I have locationManager:didRangeBeacons:inRegion: implemented and it is called when the app is running in the foreground or background, however it doesn't seem to do anything when I quit the app and lock the screen. The location services icon goes away and I never know that I entered a beacon range. Should the LocalNotification still work?
I have Location updates and Uses Bluetooth LE accessories selected in Background Modes (XCode 5) I didn't think I needed them.
Any help greatly appreciated.
-(void)watchForEvents { // this is called from application:didFinishLaunchingWithOptions
id class = NSClassFromString(#"CLBeaconRegion");
if (!class) {
return;
}
CLBeaconRegion * rflBeacon = [[CLBeaconRegion alloc] initWithProximityUUID:kBeaconUUID identifier:kBeaconString];
rflBeacon.notifyOnEntry = YES;
rflBeacon.notifyOnExit = NO;
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
[self.locationManager startRangingBeaconsInRegion:rflBeacon];
[self.locationManager startMonitoringForRegion:rflBeacon];
}
-(void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
if (beacons.count == 0 || eventRanged) { // breakpoint set here for testing
return;
}
eventRanged = YES;
if (backgroundMode) { // this is set in the EnterBackground/Foreground delegate calls
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:#"Welcome to the %# event.",region.identifier];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
// normal processing here...
}
Monitoring can launch an app that isn't running. Ranging cannot.
The key to having monitoring launch your app is to set this poorly documented flag on your CLBeaconRegion: region.notifyEntryStateOnDisplay = YES;
This can launch your app on a region transition even after completely rebooting your phone. But there are a couple of caveats:
Your app launches into the background only for a few seconds. (Try adding NSLog statements to applicationDidEnterBackground and other methods in your AppDelegate to see what is going on.)
iOS can take its own sweet time to decide you entered a CLBeaconRegion. I have seen it take up to four minutes.
As far as ranging goes, even though you can't have ranging wake up your app, you can make your app do both monitoring and ranging simultaneously. If monitoring wakes up your app and puts it into the background for a few seconds, ranging callbacks start up immediately. This gives you a chance to do any quick ranging actions while your app is still running.
EDIT: Further investigation proves that notifyEntryStateOnDisplay has no effect on background monitoring, so the above should work regardless of whether you have this flag. See this detailed explanation and discussion of delays you may experience
Code for iOS 9 to range beacons in the background, by using Location Updates:
Open Project Settings -> Capabilities -> Background Modes -> Toggle Location Updates and Uses Bluetooth LE accessories to ON.
Create a CLLocationManager, request Always monitoring authorization (don't forget to add the Application does not run in background to NO and NSLocationAlwaysUsageDescription in the app's info.plist) and set the following properties:
locationManager!.delegate = self
locationManager!.pausesLocationUpdatesAutomatically = false
locationManager!.allowsBackgroundLocationUpdates = true
Start ranging for beacons and monitoring region:
locationManager!.startMonitoringForRegion(yourBeaconRegion)
locationManager!.startRangingBeaconsInRegion(yourBeaconRegion)
locationManager!.startUpdatingLocation()
// Optionally for notifications
UIApplication.sharedApplication().registerUserNotificationSettings(
UIUserNotificationSettings(forTypes: .Alert, categories: nil))
Implement the CLLocationManagerDelegate and in your didEnterRegion send both startRangingBeaconsInRegion() and startUpdatingLocation() messages (optionally send the notification as well) and set the stopRangingBeaconsInRegion() and stopUpdatingLocation() in didExitRegion
Be aware that this solution works but it is not recommended by Apple due to battery consumption and customer privacy!
More here: https://community.estimote.com/hc/en-us/articles/203914068-Is-it-possible-to-use-beacon-ranging-in-the-background-
Here is the process you need to follow to range in background:
For any CLBeaconRegion always keep monitoring on, in background or foreground and keep notifyEntryStateOnDisplay = YES
notifyEntryStateOnDisplay calls locationManager:didDetermineState:forRegion: in background, so implement this delegate call...
...like this:
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region{
if (state == CLRegionStateInside) {
//Start Ranging
[manager startRangingBeaconsInRegion:region];
}
else{
//Stop Ranging
[manager stopRangingBeaconsInRegion:region];
}
}
I hope this helps.
You are doing two separate operations here - 'ranging' beacons and monitoring for a region. You can monitor for a region in the background, but not range beacons.
Therefore, your implementation of locationManager:didRangeBeacons:inRegion: won't get called in the background. Instead, your call to startMonitoringForRegion will result in one / some of the following methods being called:
– locationManager:didEnterRegion:
– locationManager:didExitRegion:
– locationManager:didDetermineState:forRegion:
These will get called in the background. You can at that point trigger a local notification, as in your original code.
Your app should currently wake up if you're just wanting to be notified when you enter a beacon region. The only background restriction I know of concerns actually hosting an iBeacon on an iOS device. In that case, the app would need to be physically open in the foreground. For that situation, you'd be better off just doing the straight CoreBluetooth CBPeripheralManager implementation. That way you'd have some advertising abilities in the background.