I have an app that need Location Based Services in the background.
So in info.plist I setrRequired background modes to "App registers for location updates".
and everything works fine.
My question is, how can I disable it running in background?
Is it possible to add an option and let users set it on/off?
As I know info.plist can not be change programmatically
so how can I do this?
The key is the description - it supports registering for location updates. So all you should have to do is tell your location manager to stop updating when the app goes into the background (or before) and it should then not be using location updates in the background.
There is no way to re-define the info.plist abilities of the app at runtime.
Traditionally (and pragmatically) you would set up your location manager to -startUpdatingLocation while your app is in use. This will drain your battery pretty quick if you leave it running full time. Typically you would call this, get the information you need, or update information, the call -stopUpdatingLocation. This puts everything to rest, no more battery drain.
When rolling over to the background, you have very limited options for accuracy if you still want to preserve battery life. The main one is -startMonitoringForSignificantChanges. This location update relies primarily on cell tower hand offs and triangulation. So if your user isn't moving large distances or is in an area with limited cell reception, don't expect this background mode to work very good.
You other background option is -startMonitoringRegion. This allows you to create a circle based region around places and get notifications when you -enterRegion or -exitRegion. These all have to be set up before entering the background. They do have the added benefit of better location updates. WiFi changing, cell tower handoffs, and even other apps using location updates. The OS grabs all locations updates and funnels them down and makes them available to any regions registered with the OS.
You still have the option to run location updates live in the background, but your users will not be thankful that they have no battery after 30 minutes of use.
Good luck. There are plenty of examples how to achieve all of these on SO and the web. The trick is finding the right combination that will work for you. Good Luck.
Related
This question already has answers here:
Periodic iOS background location updates
(9 answers)
Closed 5 years ago.
I have an app that is used for tracking van drivers. I need to be able to get the location (x&y) every 30 seconds or so. I have a method and a timer that allows me to do this.
I was wondering how (if at all possible) I can have this working regardless of whether the app is active or not?
I've recently started messing around with core-location so my knowledge my not be bullet-proof.
If the app is is still active but only in the background then as long as you set the allowsBackgroundLocationUpdates to true then you will get updates. But this could become battery consuming, it could go on until your battery dies.
You may want to avoid constant location checking by setting:
pausesLocationUpdatesAutomatically to true.
However if you do set it to true then if your driver stops at a location for too long(I don't know what means too long) then the app would stop completely from tracking location until you bring the app to foreground again. I'm saying that based on:
Important For apps that have in-use authorization, a pause to location
updates while in the background ends access to location updates until
the app returns to the foreground and is able to restart location
services. If you do not wish location updates to stop entirely,
consider disabling this property and changing location accuracy to
kCLLocationAccuracyThreeKilometers when your app moves to the
background. Doing so allows you to continue receiving location updates
in a power-friendly manner.
So it's a little tricky. You may want to set pausesLocationUpdatesAutomatically to true but then if your business requirements allow...do something like stopUpdatingLocation() after 1hr of app being in background to stop location updates completely.
Yes, this is possible.
If your iOS app must keep monitoring location even while it’s in the background, use the standard location service and specify the location value of the UIBackgroundModes key to continue running in the background and receiving location updates. (In this situation, you should also make sure the location manager’s pausesLocationUpdatesAutomatically property is set to YES to help conserve power.) Examples of apps that might need this type of location updating are fitness or turn-by-turn navigation apps.
For more information, check out the Location and Maps Programming Guide.
I developed an application that uses "Location Significant Change" feature. I notice that after users download my app, they quickly tend to uninstall it because they see the white GPS icon on their iPhone, and they think it will drain their phone battery.
I wonder if there is any workaround or suggestion, I wonder also if Apple is going to do anything about the GPS icon when using "Location Significant Change" feature, either on iOS 6 or next versions.
on comparison Android have a feature where you could setup Alarm manager to wake up application on certain time intervals. This will solve the problem, is there something similar on iOS (or iOS 6 maybe).
Thanks.
This is going to be a non-negotiable aspect to using location services on an iOS device. Apple wants to inform users when their location is being used so they can make informed decisions. So overriding or hiding this location icon, is not going to happen. (jailbreak excluded)
In my opinion, you have a couple options to help yourself out.
Inform the users in your app description in the app store so they know up front that the battery life isn't going to be slammed that bad.
Have a good detailed message set when you ask for permission to use the user's location. This will let them know why and make sure they accept and allow it.
If you have an FAQ on your website or inside the app, make sure you detail what the icon means and reassure the user that Apple takes battery life very seriously, and so do you.
Outside that, there isn't a lot you can do. Just inform your users best you can and go from there. Good luck.
UPDATE
I found a setting in iOS 6 under Privacy-->Location Services-->System Services all the way at the bottom, there is a user setting to hide the stats bar icon. This frees up some status bar real estate for users that don't want to see it. It doesn't mean the app isn't using location, just the icon is hidden. This is something your users would have to do as overriding this is not an option. At least it is something to share with your users.
Note that the location arrow when using significant location changes is different from the one when you are actively using location services in the background. The best thing you can do is to make sure the user understand how is their location being used, and to teach them to identify the difference between location monitoring (like geofence and significant location changes) and location tracking.
I need to calculate distance from user location to specific location, when the app is in background, and get a local notification based on that.
Background location tracking is totally possible, and I have already done that, but is it possible to execute a block of code, containing some condition checking and based on that update and get a local notification?
I am not getting any proper solution. Is there any way to do so?
Can you help me please?
Yeah, you can definitely do that. I'm doing it in an app right now. Use significant location changes, or regions, as previously suggested, to keep the pressure off the users battery as much as possible.
We observe regions (and significant location changes where regions are not supported by the device) then check a few things and fire off a local notification if needs be.
We started with a basic prototype to prove the concept and I highly recommend that approach as a way to get familiar with the location and notification frameworks.
Start with the Location Awareness Programming Guide. Most everything you need to know is in there. (Most things that aren't in there are simply impossible for an AppStore app.) See also Tracking the User's Location in the iOS App Programming Guide.
The best tool for what you're describing is likely "Shape-Based Regions." You can basically draw a box on a map and say "when the user enters or leaves this box, let me know." If at all possible, this is the tool you should use. It has the least impact on battery life.
If you absolutely cannot solve the problem any other way, it is legal to request background location delivery with startUpdatingLocation (and the appropriate background mode in Info.plist; see the above docs). An app that tracks your route while you hike would be be appropriate for this kind of setup. But you should avoid it if at all possible since it's a major battery drain.
You will want to update for significant locations in the background: http://mobile.tutsplus.com/tutorials/iphone/ios-multitasking-background-location/. Then you will want to create a location notification based on that.
My issue is that I am not sure if you can create a local notification in the background.
I need some advice for a location based iPhone app.
I'm building an app that informs the user of nearby locations. When the app is in the foreground it will use the normal didUpdateToLocation:fromLocation: method (calculates distance between user and place of interest), but when it is in the background I'm not sure what is the best way to do this.
I'm thinking of creating CLRegions from all the places of interest just before the app goes to the background. If the user enters a region the app will do a local notification and inform the user of the place of interest.
Or I could register for CL significant update, calc distance between user and all the places and notify of any close ones. Maybe not the best solution, the OS might not give me enough time to compute close places?
Has anyone any experience doing the same? any advice?
I think you should register for significant locations updates (cell change events), and then do your math on events. You can do anything on the basis of this event. As per my experience, region monitoring is also dependent on cell change events along with some extra computations which you don't need.
However you need to enable background location updates. For which you need to convince Apple that this background location services are required for your app. As Apple docs say that background location services are only for apps who provide turn by turn navigation.
how to enable background location updating
Understanding background tracking of user's location in iOS
I know with ios 4 it is possible to run application in background as per this documentation
http://developer.apple.com/library/ios/#documentation/iphone/conceptual/iphoneosprogrammingguide/BackgroundExecution/BackgroundExecution.html
this documents states that if application updates user's current location in background continuously then it is possible to run it in background.but is it necessary that we have to use only CLLocationManager for updating current location?can we user other apis like google latitude apis for updating current location?then also it is possible that application is able to run in the background forever?
As far as I understand from the document you posted, if you define the UIBackgroundModes with a value of location in your info.plist, "the system should allow the application to run as needed in the background". So, the first answer is yes, the application can run in the background and it will run indefinitely (up to battery life). This could be easily checked, actually.
Anyway, it seems to me that this kind of functionality is "reserved" to GPS-like apps and that Apple is really concerned about its usage:
For applications that require more precise location data at regular intervals, such as navigation applications, you need to declare the application as a continuous background application. This option is available for applications that truly need it, but it is the least desirable option because it increases power usage considerably.
so, I understand that Apple will screen really thoroughly all apps that activate this mode in order to assess if they really need the continuous update or do not (and in this case, possibly, the app would not be let in into the App Store).
Now, to answer the second part of your question, I think that one way that Apple will know if your app really complies with the rules, is its usage of CLLocationManager. The risk is that if you use another service, then Apple could think when reviewing your app that you just need background time without needing to constantly update the location.
But this is just a guess...