Install the plugin using Cordova CLI
$ cordova plugin add com.crowdconnected.colocator.cordovaplugin
Change the permission descriptors in info.plist file of your iOS project
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>YOUR_TEXT_GOES_HERE</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>YOUR_TEXT_GOES_HERE</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>YOUR_TEXT_GOES_HERE</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>YOUR_TEXT_GOES_HERE</string>
If you will use Colocator for Indoor Services, also change the following permission descriptors accordingly
<key>NSBluetoothAlwaysUsageDescription</key>
<string>YOUR_TEXT_GOES_HERE</string>
<key>NSMotionUsageDescription</key>
<string>YOUR_TEXT_GOES_HERE</string>
For more details about these permissions in the iOS section
Request location permission using cordova.plugins.diagnostic or other preferred method.
Cordova CLI:
$ cordova plugin add cordova.plugins.diagnostic
JS file:
cordova.plugins.diagnostic.requestLocationAuthorization(YOUR_HANDLE_LOCATION_AUTHORIZATION_STATUS_FUNCTION, YOUR_ERROR_FUNCTION);
ColocatorWrapper.triggerBluetoothPermissionPopUp(YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION)
ColocatorWrapper.triggerMotionPermissionPopUp(YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION)
Start the library when the application starts using:
ColocatorWrapper.start("YOUR_APP_KEY", YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION);
In iOS the library should be started in AppDelegate.m as follows:
#import <CCLocation/CCLocation-Swift.h>
...
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
...
[CCLocation.sharedInstance startWithApiKey:@"YOUR_APP_KEY" urlString:@"URL_STRING"];
...
}
We use background refresh to periodically check whether we should enable location tracking.
Enable Background Refresh by registering for events in AppDelegate.m:
Import CCLocation Obj-C header
#import <CCLocation/CCLocation-Swift.h>
Set the minimum time interval for background refresh
[[UIApplication sharedApplication] setMinimumBackgroundFetchInterval:60];
Calling the library at background refresh
-(void)application:(UIApplication *)application performFetchWithCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
// At background refresh, the CCLocation library should be notified to update its state
[CCLocation.sharedInstance updateLibraryBasedOnClientStatusWithClientKey:@"YOUR_APP_KEY" isSilentNotification:false completion:^(BOOL result) {}];
}
We use Silent Push Notifications to enable location tracking at a given time.
Please enable Silent Push Notifications in AppDelegate.m by:
[[UIApplication sharedApplication] registerForRemoteNotifications];
- (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
// For CCLocation messaging feature, send device token to the library as an alias
NSString * deviceTokenString = [[[[deviceToken description]
stringByReplacingOccurrencesOfString: @"<" withString: @""]
stringByReplacingOccurrencesOfString: @">" withString: @""]
stringByReplacingOccurrencesOfString: @" " withString: @""];
[CCLocation.sharedInstance addAliasWithKey:@"apns_user_id" value:deviceTokenString];
}
NSDictionary *apsInfo = [userInfo objectForKey:@"apsInfo"];
NSString *source = [apsInfo objectForKey:@"source"];
if ([source isEqualToString:@"colcoator"]) {
[CCLocation.sharedInstance receivedSilentNotificationWithUserInfo:userInfo clientKey:@"YOUR_APP_KEY" completion:^(BOOL result) {}];
}
If required you can stop the library in your JS file
ColocatorWrapper.stop(YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION);
Aliases are key-value pairs. They are primarily used to send alternative IDs for a device, for example a unique Push Notification identifier. Go to the Push Notification Integrations page for more details
ColocatorWrapper.addAlias("YOUR_KEY", "YOUR_VALUE", YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION);
For indoor location and positioning, a few more steps need to be implemented
The latest location can be sent back to the device either as a one-off or a stream of updates. This is primarily used for displaying the location of the device on a map.
The location update callbacks contain a JSON object.
Request one location:
ColocatorWrapper.requestLocation(YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION)
Request a stream of location updates:
ColocatorWrapper.registerLocationListener(YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION)
Please make sure you unregister from location callbacks when the user is not using the indoor map screen or navigation.
This is important for battery and performance optimization.
Stop the stream of updates:
ColocatorWrapper.unregisterLocationListener(YOUR_CALLBACK_FUNCTION, YOUR_ERROR_FUNCTION)