How to stop running app in background when not needed? - ios

In my app I'm using location updates in the background and this works without a problem. The issue is when the user doesn't need the location updates and goes out of the app, the app is still running in the background. I added some code to the appdelegate method:
- (void)applicationDidEnterBackground:(UIApplication *)application
Here I set location manager to stop updating location and also set it to nil. This doesn't solve the problem.
Is there a way for the app to behave like a normal app when the user doesn't need background updating? In this version is frustrating for the user to see the location arrow in the status bar even if the location is not updating. The location manager is stored in a singleton, so there is only one instance of manager in the whole app.

Related

Stop Updating Locations on ios when user clicks stop button

I am using location services in my ios app for tracking purposes. The app tracks location in foreground as well as background, until the app terminates.
Here all the code is written in react-native java script except the beacon tracking API values which are being sent from native ios code through app delegate and location manager.
The app updates locations in all cases throughout the life cycle of app but sends beacon values to API only after a specific track interval. There is a stop button, on clicking of which the app stops sending location values to API but the location manager still continues to change lat long values.
What i want is, the clicking of stop button should stop the location manager updates and should start only when i click on 'start' button. This can be handled through app delegate methods but they are called only when any app transition happens like switching from foreground to background or vice versa.
They are not called if i switch between screens remaining on foreground only. And all the screens are handled through react code only, not in native ios.
Suggest something to control the location manager activities in the manner mentioned above, please.

I want func locationManager(manager: CLLocationManager, didUpdateToLocation to be called even after app is killed?

After my app is killed I want to relaunch the app based on the location.speed paramater, I mean to say when the device speed is>5kmph I want my app to get open and one button needs to be clicked programatically?
NOT POSSIBLE.
You can not execute part of code when you process is killed. Its as simple as that.
You can do this in android with background sevice which run even when you app gets killed because background service is different process. But iOS don't allow to create background service.
Set allowsBackgroundLocationUpdates = true
The default value is NO for allowsBackgroundLocationUpdates
If your app uses location in the background (without showing the blue status bar) you have to set allowsBackgroundLocationUpdates to YES in addition to setting the background mode capability in Info.plist. Otherwise location updates are only delivered in foreground. The advantage is that you can now have location managers with background location updates and other location managers with only foreground location updates in the same app. You can also reset the value to NO to change the behavior.

App restarts when woken from background

G'day iOS Guru's,
I have searched extensively for an answer, but can't find one (I bet the first response to my question will be to another similar question, but I cant find it).
Anyway, my problem is that I am running a simple map app that the user can drop pins on the map with a customised circle overlay around the dropped pin.
When the app goes into the background (iphone locked or home button pressed), if I re-enter the app within ~ 5 mins, the pins are still there and the app reopens to the last screen.
All good.
However, if I leave the app in the background for longer than 5 mins, the app restarts and all the pins are lost.
I have "Application does not run in background = NO" in the plist, and also enabled "App registers for location updates" under Required background modes.
How can I prevent the app from restarting after it enters the background and load the last opened view?
iOS can, and will, terminate your app while it's in the background if it needs additional resources to carry out whatever's going on in the foreground.
You need to ensure that your data is saved/archived when your app is terminated, and unpacked when re-launched so as to go back to where the user last was. The traditional way to do this is to use the applicationDidEnterBackground method, which is called when your app is suspended. You can then save all the data you need in order to resume cleanly in case your app is later terminated.
However, if you're targeting iOS 6 and upwards you can take advantage of a new feature - State Presumption & Restoration (link to documentation). State restoration off-loads some (but not all) of the heavy lifting onto iOS, and it can automatically snapshot your UI and provides easier ways to preserve and restore data.
in your AppDelegate.m
- (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.
// Save your data
}

How to force ViewController to unload?

I have an application that runs on background because it's needs to use Location Update. however, since i don't want to consume battery. I would like to make sure that all view controller is unloaded when app switch to run on background.
Forcing your view controller to unload is not going to save any battery life. It's best to let the operating system worry about taking your app out of memory when it needs to. If you're using the background mode that notifies your application when the user has travelled a significant amount you need to worry about this even less since your app is not actually running in the background but only given a chance to respond to location changes. If you're using the constant location background mode (for GPS apps, etc.) this is a little bit more of an issue.
You don't need to unload the view controllers, but you do need to turn off location updates. If you are starting location updates in your App Delegate, put in something like this:
- (void)applicationWillResignActive:(UIApplication *)application
{
[myLocationManager stopUpdatingLocation];
}
You can also do this in the viewWillDisappear: method if your location manager is started in a different place than the App Delegate.

CLLocationManager don't stop

Dear fellow developers,
I am trying hard to find a solution for my problem regarfing CLLocationManager.
I use a CLLocationManager instance in my Application. If the user selects the Home button on the device or terminates the application I want the location services to stop.
Therefor I call [self.locationManager stopUpdatingLocation]; - But this somehow doesn't work. The application enters the background and the small location arrow in the upper right corner of the status bar don't disappear. Even if I add [self.locationManager release] or self.locationManager.delegate = nil; - the location tracking don't stop :-/
It only disappears if I go to my device settings and switch off location services for the app. Whenever I switch back to location service enabled I immediately get a purple colored arrow next to the switch toggle and the icon reappears in the status bar.
My question is how can I turn off location services when the app enters the background or is terminated?
Thanks a lot in advance and have a nice day :-)
Your location is disabled. The location service icon "meaning" has changed on iOS 5. Take a look at this question: https://apple.stackexchange.com/questions/27463/why-is-the-location-services-icon-always-present
I Quote the answer:
It's a new feature in ios 5 called "region Monitoring"
The reason it's active even if the app is closed is that this feature
runs in iOS 5 core and notifies all apps that are registered when they
have entered or left a specific geo-fence.
Reminders does that when you use a location based reminder.
Although the location icon appears at all time. This actually has very
minimal impact on the battery due to apple really optimizing this
feature by using cell and wifi mostly.
Your app is working ok. The system behaviour is the one who changed.
You do it the right way. When entering background, it's ok if some delegate methods are called for some seconds. That should stop.
Where do you stop the location updates ? Are you sure it is triggered ? If yes, are your delegate method called even if the visual indicators tell something else ?
Are you sure you don't trigger a method that reactivate the location update after you have stopped it (because for example you can receive some updates even after stopped).
For instance if you started monitoring a significant location change, then you should unsubscribe from it with the corresponding pair method. If you are using region enter, then until you unsubscribe, the system will notify your delegates.

Resources