info |
Important
|
How to enable Location Triggered?
Required Permissions:
Region Monitoring(Geofences) requires Always Authorization and Precise location accuracy to be enabled to work. Therefore make sure that the app is configured to get these permissions and also it's a good practice to let the user know the context in which these permissions are needed, this will also encourage the user to provide these permissions.
SDK Installation
Install using CocoaPod
Integrate the MoEngageGeofence framework by adding the dependency in the podfile as show below.
pod 'MoEngageGeofence','~>4.2.0'
Now run pod install
to install the framework
Install using Swift Package Manager
MoEngageGeofence is supported through SPM from SDK version 4.2.0. To integrate use the following git hub url link and set the branch as master or version as 4.2.0 and above https://github.com/moengage/MoEngage-iOS-Geofence.git
Manual Integration
To integrate the MoEngageGeofence
SDK manually to your project follow this doc.
warning |
Note MOGeofence has been renamed to MoEngageGeofence from version 4.2.0.Do update the podfile and import statement accordingly. |
Start Geofence Monitoring:
After integrating the MOGeofence module call startGeofenceMonitoring()
method to initiate the geofence module. This will fetch the geofences around the current location of the user.
MOGeofence.sharedInstance.startGeofenceMonitoring()
[[MOGeofence sharedInstance] startGeofenceMonitoring];
Geofence Handler also has callbacks for didEnterRegion
and didExitRegion
. You can get these by confirming to the as MOGeofence.sharedInstance.setGeofenceDelegate(self)
extension GeofenceViewController: MOGeofenceDelegate {
func geofenceEnterTriggered(withLocationManager locationManager: CLLocationManager?, andRegion region: CLRegion?, forAccountMeta accountMeta: MOAccountMeta) {
print("Geofence Entered")
}
func geofenceExitTriggered(withLocationManager locationManager: CLLocationManager?, andRegion region: CLRegion?, forAccountMeta accountMeta: MOAccountMeta) {
print("Geofence Exited"
}
}
@interface MyViewController ()<MOGeofenceDelegate>
---
- (void)geofenceEnterTriggeredWithLocationManager:(CLLocationManager * _Nullable)locationManager andRegion:(CLRegion * _Nullable)region forAccountMeta:(MOAccountMeta * _Nonnull)accountMeta {
NSLog(@"Geofence Entered");
}
- (void)geofenceExitTriggeredWithLocationManager:(CLLocationManager * _Nullable)locationManager andRegion:(CLRegion * _Nullable)region forAccountMeta:(MOAccountMeta * _Nonnull)accountMeta {
NSLog(@"Geofence Exited");
}
Testing Geofencing
First, create a geofencing campaign on your MoEngage dashboard. You can test geofencing in the following ways:
- On the simulator:
- You can simulate location as shown below.
- Simulate the location for which you have created the campaign on the dashboard.
If you get the respective call back (the delegate methods in MOGeofenceHandler), you are good to go. On simulator, you will not receive push notifications.
- You can simulate location as shown below.
-
On the device:
-
You can simulate location for real device from the bar above the console as shown below:
- You can add a gpx file with the locations configured. A sample gpx file looks like this:
<?xml version=“1.0”?> <gpx version=“1.0”> <wpt lat=“12.9719” lon=“77.6412”><name>CustomName</name></wpt> <wpt lat=“12.9619” lon=“77.6312”></wpt> <wpt lat=“12.9519” lon=“77.6112”></wpt> </gpx>
-
On the device, once you get the delegate callback for entering or exit in a region, a notification will be sent to the device. This happens instantly, but the notification might take up to 10 minutes.
Geo Notifications
Once you have received the notification, to identify geo notifications, there is a custom param “cType” = “geo” in the param app_extra, as shown below:
{
"app_extra" = {
cType = geo;
screenData = {
"" = "";
};
screenName = "";
};
aps = {
alert = "exit london- single fence";
badge = 1;
};
moengage = {
cid = "55a628bcf4c4073bb66a368b_GEO:55a628bcf4c4073bb66a368c_ABab1:2015-07-15_14:11:33.704706";
};
}