EngineeringDecember 02, 2014

Location services best practices

Many mobile apps can greatly improve their users’ experience by making use of information on users’ whereabouts. A hotel app…

Many mobile apps can greatly improve their users’ experience by making use of information on users’ whereabouts. A hotel app can greet a guest and start the check-in process as soon as she enters a geo-fence. A parking meter app can present the current location and ask for the parking spot number rather than forcing users to browse a list or filter by zip code. Think of how handy a turn-by-turn navigation system is, and you get a sense of the value of accessing a user’s location.

Knowing a user’s location also can raise privacy concerns, a discussion that is largely beyond the scope of this article. Suffice it to say that if your app does not use geolocation properly to provide users with tangible benefits, users can revoke location privileges or even delete your app. Clearly that is not a desired outcome.

Let’s take a look at 3 best practices working with a user’s location:

Ask Permission at the Right Time

Instead of asking for a user’s permission immediately after your app has finished launching, consider deferring the request until a user taps on an element in the app that requires it. By letting users get acquainted with your app, it is more likely they will grant you permission to use their location. In addition, it is a good practice to present a brief and descriptive courtesy message to your users explaining the benefit of allowing your app to use their location.

Use data at the right time

Prior to iOS 8, app developers were advised to first check the authorization status to access a user’s location, and if authorized, invoke the Core Location method to start updating location.
if ([CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorized) {
[self.locationManager startUpdatingLocation];
Can I pay by app?

With the introduction of iOS 8, Apple has added finer-grained options to the process of requesting a user’s location. Your app can request access to a user’s location only when it is in use, or your app may request constant access to their location.

Choose appropriately. For example, say a user wants to use your mobile payment app after taking a pet gila monster to the vet for an ultrasound. Your app only needs to know the user’s location at the vet’s office during the checkout process. On the other hand, the navigation app that gave the user driving instructions to the pet shop needs access to the user’s location at all times.

Use mParticle for more granular control

Applicable to iOS 8 or above, if you have configured your app’s Info.plist with the NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription keys, the mParticle SDK will take advantage of that and request the appropriate authorization, assuming your app has not being authorized yet and the authorization status in the Settings app is not Denied or Restricted.

NSDictionary *mainBundleInfoDictionary = [[NSBundle mainBundle] infoDictionary];

if (authorizationRequest == MPLocationAuthorizationRequestAlways &&
[self.locationManager respondsToSelector:@selector(requestAlwaysAuthorization)] &&
[self.locationManager requestAlwaysAuthorization];
} else if (authorizationRequest == MPLocationAuthorizationRequestWhenInUse &&
[self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)] &&
[self.locationManager requestWhenInUseAuthorization];
} else {
[self.locationManager startUpdatingLocation];
In addition to this refinement in iOS 8, and irrespective of iOS version, the mParticle SDK provides a property to give you fine-tuned control using a user’s location. The backgroundLocationTracking property governs the internal behavior of the mParticle SDK and enables or disables the inclusion of location information to events when your app is running in the background.

If backgroundLocationTracking is in the reset state (NO), the mParticle SDK will disable its internal instance of the location manager (CLLocationManager) when it detects the application going to the background, and it will restore and resume tracking location once the app is brought back to the foreground. The default value is the set state (YES), which means that once beginLocationTracking:minDistance: is called, the mParticle SDK will not disable its internal instance of the location manager when an app goes to the background.
MParticle *mParticle = [MParticle sharedInstance];

[mParticle beginLocationTracking:1000 minDistance:10];
mParticle.backgroundLocationTracking = NO;
The code above begins tracking a user’s location, but only when an app is in the foreground.

With great experience comes great value

Knowing and using a user’s location can enable your app to provide immeasurable value to users. In exchange, you need to handle this information in a manner that respects the user’s privacy and choices. The mParticle SDK gives you great flexibility to respectfully and responsibly fine tune the controls of when and where to track a user’s location. Use it wisely.

Latest from mParticle

MACH Alliance and mParticle featured image


Leading the next generation of CDP solutions: mParticle celebrates acceptance into the MACH Alliance

Madeleine Doyle – April 16, 2024
A stock image of a woman hiking with the onX logo

How onX accelerated marketing campaigns with mParticle's AI engine

April 17, 2024
A headshot of mParticle's Director of Digital Strategy & Business Value, Robin Geier


Introducing Robin Geier: mParticle's Director, Digital Strategy and Business Value

Robin Geier – April 16, 2024
M&S Customer Story

How Marks & Spencer drove revenue growth with mParticle's real-time segmentation and personalization

February 26, 2024

Try out mParticle

See how leading multi-channel consumer brands solve E2E customer data challenges with a real-time customer data platform.

Explore demoSign upContact us

Startups can now receive up to one year of complimentary access to mParticle. Receive access