iOS location services

Setting up geolocation regions and location-based messaging in Leanplum

Use the below steps to implement the Leanplum location services.

STEP 1: Add Location Services to your Podfile

To set up location services for iOS, you'll have to first add our location framework to your Podfile or as an additional linked framework:

pod 'Leanplum-iOS-Location'
pod 'Leanplum-iOS-LocationAndBeacons'

🚧

If you need iBeacons as well, include our Location and Beacons framework instead.

For a manual install, you will need to add CoreLocation to Build Phases -> Link Binary With Libraries.

STEP 2: Import LeanplumLocation and Get permission from users

On iOS, your app must get permission from a user to access their location while using your app. Our SDK automatically asks a user for permission on your behalf. You simply need to import the new Leanplum Location library into your AppDelegate, and add NSLocationWhenInUseUsageDescription to your info.plist file.

import LeanplumLocation
#import <LeanplumLocation/LPLocationManager.h>

To disable the automatic iOS prompt, set the authorizeAutomatically property of LPLocationManager to false before calling start() as shown below

LPLocationManager.shared().authorizeAutomatically = false;
Leanplum.start()
[LPLocationManager sharedManager].authorizeAutomatically = NO;
[Leanplum start];

If you've disabled automatic authorization, you can request permission using the authorize method. If you want to check if the user has already given permission, you can use the needsAuthorization() method.

if(LPLocationManager.shared().needsAuthorization) {
  LPLocationManager.shared().authorize()
}
LPLocationManager * LPLocation = [[LPLocationManager alloc] init];
if(LPLocation.needsAuthorization){
  [LPLocation authorize];
}

❗️

iOS will only allow you to request access to a user's location once. After that, the user must navigate to the privacy settings for your app in Settings.

STEP 3: Add Keys to info.plist file to trigger messages

To trigger in-app messages and push notifications based on the geofence and iBeacon regions, you'll need to add a few of the below keys in your info.plist file:

  1. Add the key NSLocationWhenInUseUsageDescription: This text will appear to the user when prompting for permissions to enable location monitoring in the foreground on iOS 8+. (Required for in-app messages)
  2. Add the key NSLocationAlwaysUsageDescription: This text will appear to the user when prompting for permissions to enable location monitoring in the background on iOS 8+. (Required for push notifications)
  3. Add the key NSLocationUsageDescription: This text will appear to the user when prompting for permissions to enable location monitoring in the foreground or background on iOS 7. (Optional for in-app messaging and push notifications)
  4. Add the key Required background modes if it does not already exist. Within it, add a background mode App registers for location updates. (Required for push notifications)

Other Use Cases of Leanplum Location

Disabling location collection

If you do not want to send GPS/Cell location to Leanplum, then do not include either of the location frameworks above. Alternatively, you can include the frameworks and call disableLocationCollection before start.

Leanplum.disableLocationCollection()
[Leanplum disableLocationCollection];

Manually set user location

Our SDK now includes a method that allows you to set user location manually. This can be achieved by calling setDeviceLocationWithLatitude after Lenaplum.start(). You should call the disableLocationCollection method from above before setting the user location.

//example call
Leanplum.setDeviceLocationWithLatitude(40.748817, longitude: -73.985428)

📘

iOS limits the number of locations that an app can save to 20, however, our SDK manages this for you by only storing the nearest 20 locations to each user's device at any given time. This way, you can have more than 20 locations saved in Leanplum, but your users will only have the 20 most relevant to them (i.e. the closest).

Region monitoring is available in iOS 7 and higher.