Advice on determining close locations while iPhone app in background - iphone

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

Related

iOS background location updates

I have created an app which uses location updates in the background. I have submitted my app to apple but it got rejected by apple on the ground that you are using location updates in background rather than using significant location update and shape based region monitoring.
My app takes location updates and provided user with specific updates, which works perfectly for my scenarios and have made code to better save battery. With Apple's suggested approach the problem I'm getting with significant location update and shape based region monitoring is location accuracy. With significant location update I get events fired for all the region within that cell tower range or say 100 m - 3000 m rather than when user enters a region say lat long with 100 m radius.
I've had many conversations with apple and they don't seem to care about the developers and new technologies they are creating.
Is there any developer who faced same problem of using background location update to do something other than navigation and got accepted or has anyone used significant location update with shape based monitoring to provide precise updates with better location accuracy.
Any help is much much appreciated.
Varun welcome to SO community.
Here is some lines from docs
Any app that requests background location services should use those services to provide a tangible benefit to the user. For example, a turn-by-turn navigation app would be a likely candidate for background location services because of its need to track the user’s position and report when it is time to make the next turn.
So if you are using standard location service and whenever you get updates and you are not doing anything with that update (in terms of user not coding) then there are very high chances that Apple may reject your app, which they already did.
So what's the solution then?
1. Answer is try submitting the app with other approach.
2. Try submitting the app with other name :D
Solution Number 1. Instead of using standard services use significant location service which gives the accuracy of around hundreds of meters say 200-1700 m and track if user is moving near to your spot or far away from your spot.
If user is moving near to your spot and user is around 500m (depends upon you) near to your spot just start standard location services and track where user is going if user enters you spot then show user "Catch you" or if user moves away from your spot (>500m) then stop standard location service and start significant location service.

How to receive acceleration-related data from the onboard hardware, when application in background mode?

Am developing application for calculate count of user moved steps and draw the user activities in the map. And we are using UIAccelerometer delegate for receive acceleration-related data from the onboard hardware. So I need to receive acceleration-related data when the application in background mode too. Last time one of my application got rejected because of using location service in background, Apple suggested me like "you can only use this background mode if your app truly needs this information to provide value for the user". Kindly suggest the best approach for this application.
You cannot run accelerometer in background.
You can track user's location in background... however, if you just keep tracking it and do nothing with that info.. apple is going to reject it and tell you to just get the lates location when app comes to foreground.
what you can try is.. update the user of total distance covered etc on a regular basis (like run keeper) and this will justify tracking location in background.
But first, you can try appealing to the review board explaining that you need to track location in background because you show entire route travelled by user when app comes to foregraound. and compare this to existing apps like run keeper and if you are lucky apple may approve your application without any changes...
This can easily be done.
What happens in that iOS put your application in halt state when your application is not in running in foreground state.
You just need to register your application for background execution.
Refer :
http://dcraziee.wordpress.com/2013/05/20/background_ios/
Also refer :
iOS background application network access
for apple policies about using location service.Which states that you can use location service in background.

Is it possible to get distance from user location to specific location in background in IOS?

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.

How to do hourly location updates?

I'm building a location-based app, but I hate having the location services icon on. I only need coordinates once per hour, but I can't figure out how to do this without saying the app is a VoIP app. Is it possible to do this in a way that's App Store acceptable?
You can use location in the UIBackgroundModes of your app, see the documentation.
About the location services icon on, I think you should leave it. It's a fair feedback for your user. A user has to know its position is being tracked and leaving the icon on for a few seconds each time the geolocation actually happens wouldn't be fair. (and it sounds like a case of app rejection to me).
As long as you don't have a negative impact on the device battery, the user will be grateful you let it know you're tracking his position.
For instance, apps such as OpenPaths made my iPhone show the location icon on all the time and have no real negative impact on the battery.
I would register for significant location change updates. When updates happen compare timestamp with last update. Use update if time delta > 60 minutes. Worst case: I believe you get the location indicator outline. this tells the user that a kind of tracking is happening — but not all the time, so it' not a battery issue and you don't get too many support requests. Best case: Registering for significant location change update doesn't show the indicator outline. But I am not certain. Completely without icon might just not be possible.

Programmatically change Required background modes

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.