Utility Methods

An archive of our SDK methods.

Initialize SDK

Initialize the SDK in your AppDelegate

Swift
Objective-C
Swift
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
GeoSpark.intialize("PUBLISHABLEKEY")
return true
}
Objective-C
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[GeoSpark intialize:@"PUBLISHABLEKEY"];
return YES;
}

Set DeviceToken

GeoSpark SDK is capable of sending push notifications to your users. Check here to get device token.

Swift
Objective-C
Swift
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
GeoSpark.setDeviceToken(deviceToken)
}
Objective-C
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
[GeoSpark didRegisterForRemoteNotificationsWithDeviceToken:deviceToken];
}

Create User

GeoSpark SDK needs an User ID object to identify the device. Before creating user you should have device token as you cannot create user without device token.

Swift
Objective-C
Swift
GeoSpark.createUser("User Description",{ (user) in
// user.userId
},onFailure: { (error) in
// error.errorCode
// error.errorMessage
})
Objective-C
[GeoSpark createUser:@"User Description" :^(GeoSparkUser * user) {
// user.userId
} onFailure:^(GeoSparkError * error) {
// error.errorCode
// error.errorMessage
}];

Get User

If you already have an User ID, then SDK has a convenience method getUser() to start the session for the existing user.

Swift
Objective-C
Swift
GeoSpark.getUser("USER_ID", { (user) in
// user.userId
},onFailure: { (error) in
// error.errorCode
// error.errorMessage
})
Objective-C
[GeoSpark getUser:@"USER_ID" :^(GeoSparkUser * user) {
// user.userId
} onFailure:^(GeoSparkError * error) {
// error.errorCode
// error.errorMessage
}];

User Description

You can set Description for user by using setDescription method.

Swift
Objective-C
Swift
GeoSpark.setDescription("Description", { (user) in
// user
}, onFailure: { (erorr) in
// error.errorCode
// error.errorMessage
})
Objective-C
[GeoSpark setDescription:@"Description" :^(GeoSparkUser * user) {
// user
} onFailure:^(GeoSparkError * error) {
// error.errorCode
// error.errorMessage
}];

Set Events

Use this method after user creation to enable user events for geofence, activity and trips. By default, all user events will be disabled post user creation.

Swift
Objective-C
Swift
GeoSpark.toggleEvents(Geofence: Bool, Trip: Bool, Activity: Bool, { (events)
in
// events.geofenceEvents
// events.activityEvents
// events.tripsEvents
}) { (error) in
//error.errorCode
//error.errorMessage
}
Objective-C
[GeoSpark toggleEventsWithGeofence:true Trip:true Activity:true
:^(GeoSparkEvents * events) {
// events.geofenceEvents
// events.activityEvents
// events.tripsEvents
} onFailure:^(GeoSparkError * error) {
//error.errorCode
//error.errorMessage
}];

Get Events

Use this method after user creation to get the current status of user events for geofence, activity and trips.

Swift
Objective-C
Swift
GeoSpark.getEventsStatus({ (events) in
// events.geofenceEvents
// events.activityEvents
// events.tripsEvents
}) { (error) in
//error.errorCode
//error.errorMessage
}
Objective-C
[GeoSpark getEventsStatus:^(GeoSparkEvents * events) {
// events.geofenceEvents
// events.activityEvents
// events.tripsEvents
} onFailure:^(GeoSparkError * error) {
//error.errorCode
//error.errorMessage
}];

Check Location Permission

Check whether your App has location permission.

Swift
Objective-C
Swift
GeoSpark.isLocationEnabled()
Objective-C
[GeoSpark isLocationEnabled];

Check Motion Permission

Check whether your App has motion permission.

Swift
Objective-C
Swift
GeoSpark.isMotionEnabled()
Objective-C
[GeoSpark isMotionEnabled];

Request Location Permission

Check whether your App has location permission.

Swift
Objective-C
Swift
GeoSpark.requestLocation()
Objective-C
[GeoSpark requestLocation];

Request Motion Permission

Check whether your app has motion permission.

Swift
Objective-C
Swift
GeoSpark.requestMotion()
Objective-C
[GeoSpark requestMotion];

Start Location Tracking

You can start tracking the user location by using startTracking() method.

Swift
Objective-C
Swift
GeoSpark.startTracking()
Objective-C
[GeoSpark startTracking];

Check Location Tracking

Check whether location tracking is started or not. This method returns boolean value.

Swift
Objective-C
Swift
GeoSpark.isLocationTracking()
Objective-C
[GeoSpark isLocationTracking];

Stop Location Tracking

You can stop tracking the user location by using stopTracking() method.

Swift
Objective-C
Swift
GeoSpark.stopTracking()
Objective-C
[GeoSpark stopTracking];

Location listener

To listen for location client-side in the background, create a class that implements GeoSparkDelegate and then call GeoSpark.delegate .

Set your GeoSparkDelegate in a codepath that will be initialized and executed in the background. For example, make your AppDelegate implement GeoSparkDelegate , not a ViewController. AppDelegate will be initialized in the background, whereas a ViewController may not be.

Swift
Objective-C
Swift
AppDelegate.swift
class AppDelegate: UIResponder,
UIApplicationDelegate,UNUserNotificationCenterDelegate,GeoSparkDelegate{
var window: UIWindow?
func application(_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey:
Any]?) -> Bool {
GeoSpark.delegate = self
return true
}
....
func didUpdateLocation(_ location: GSLocation) {
//do something with location, user
}
....
}
Objective-C
AppDelegate.h
#import <GeoSpark/GeoSpark.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate,GeoSparkDelegate>
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:
(NSDictionary *)launchOptions {
GeoSpark.delegate = self;
return YES;
}
....
- (void)didUpdateLocation:(GSLocation *)location {
//do something with location, user
}

Current Location

Get current location of the user. You can set the accuracy from 10 to 100 meters (default is 10).

Swift
Objective-C
Swift
GeoSpark.getCurrentLocation(accuracy) { (location) in
//location.latitude
//location.longitude
//location.activity
//location.accuracy
//location.userId
}
Objective-C
[GeoSpark getCurrentLocation:10:^(GSLocation * location) {
//location.latitude
//location.longitude
//location.activity
//location.accuracy
//location.userId
}];

Update Current Location

Using updateCurrentLocation method, one can update user current location, you can set the accuracy from 10 to 100 meters (default is 10).

Swift
Objective-C
Swift
GeoSpark.updateCurrentLocation(accuracy)
Objective-C
[GeoSpark updateCurrentLocation:accuracy];

This method should be used only if you need to update the current location of the device with better accuracy. Using this method often might consume battery.

Start Trip

Once user start tracking location, you can use startTrip method

Swift
Objective-C
Swift
GeoSpark.startTrip("Trip Id","Trip Description", { (trip) in
// trip.msg
}, onFailure: { (error) in
// error.errorCode
// error.errorMessage
})
Objective-C
[GeoSpark startTrip:@"Trip Id":@"Trip Description" :^(GeoSparkTrip * trip) {
// trip.msg
} onFailure:^(GeoSparkError * error) {
// error.errorCode
// error.errorMessage
}];

End Trip

Once the trip started, you can stop the trip using TRIPID

Swift
Objective-C
Swift
GeoSpark.endTrip("TRIPID", { (trip) in
// trip.msg
}, onFailure: { (error) in
// error.errorCode
// error.errorMessage
})
Objective-C
[GeoSpark endTrip:@"TRIPID" :^(GeoSparkTrip * trip) {
// trip.msg
} onFailure:^(GeoSparkError * error) {
// error.errorCode
// error.errorMessage
}];

Active Trips

By using activeTrips method you can see active trips of the user.

Swift
Objective-C
Swift
GeoSpark.activeTrips({ (trip) in
// trip.trips[0].trip_id
// trip.trips[0].isDeleted
// trip.trips[0].isEnded
// trip.trips[0].isStarted
// trip.trips[0].createdAt
// trip.trips[0].updatedAt
}) { (error) in
// error.errorCode
// error.errorMessage
}
Objective-C
[GeoSpark activeTrips:^(ActiveTripsV2Response * trip) {
// trip.trips[0].trip_id
// trip.trips[0].isDeleted
// trip.trips[0].isEnded
// trip.trips[0].isStarted
// trip.trips[0].createdAt
// trip.trips[0].updatedAt
} onFailure:^(GeoSparkError * error) {
// error.errorCode
// error.errorMessage
}];

TrackLocationInAppState

Using trackLocationInAppState method, one can configure the SDK settings for enabling location tracking during given App states.

Swift
Objective-C
Swift
GeoSpark.trackLocationInAppState([SETTINGS])
Objective-C
[GeoSpark trackLocationInAppState:@[SETTINGS]];

Parameter

Description

SETTINGS

GSAppState.AlwaysOn,GSAppState.Background, GSAppState.Foreground,GSAppState.Terminated

TrackLocationInMotion

Using trackLocationInMotion method, one can configure the SDK settings for enabling motion tracking.

Swift
Objective-C
Swift
GeoSpark.trackLocationInMotion([SETTINGS])
Objective-C
[GeoSpark trackLocationInMotion:@[SETTINGS]];

Parameter

Description

SETTINGS

GSMotion.All, GSMotion.Stationary, GSMotion.Running, GSMotion.Walking,GSMotion.AutoMotive

setLocationAccuracy

Using setLocationAccuracy method, one can set location tracking with accuracy (5 to 100 meters).

Swift
Objective-C
Swift
GeoSpark.setLocationAccuracy(accuracy)
Objective-C
[GeoSpark setLocationAccuracy:accuracy];

Notification Opened Handler

By using this method inside Notification delegate method, track the campaigns impressions and counts.

Swift
Objective-C
Swift
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void)
{
GeoSpark.notificationOpenedHandler(response)
completionHandler()
}
Objective-C
- (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void (^)(void))completionHandler
{
[GeoSpark notificationOpenedHandler:response];
completionHandler();
}

Logout User

You can logout user by using logoutUser method.

NOTE : You need to reinitialize the SDK in case you want to login again.

Swift
Objective-C
Swift
GeoSpark.logoutUser({ (message) in
// message
}) { (error) in
// error.errorCode
// error.errorMessage
}
Objective-C
[GeoSpark logoutUser:^(NSString * message) {
// message
} onFailure:^(GeoSparkError * error) {
// error.errorCode
// error.errorMessage
}];