datacollector-ios-sdk
Telenav DataCollector iOS SDK.
Installation
Preparations
Install CocoaPods on your computer:
sudo gem install cocoapods
For more information follow CocoaPods Installation Guides
Installation
Create a Podfile, and add your dependencies:
target 'MyAppTarget' do use_frameworks! end
For more information follow Podfile Guides
Add the following lines to the beginning of Podfile.
plugin 'cocoapods-art', :sources => [ 'telenav-cocoapods' ]
Add DataCollector dependency in your target (after
use_frameworks!
):pod 'TelenavSDKDataCollector'
Run
pod repo-art update telenav-cocoapods
in your project directory.And then:
pod install
.Open
App.xcworkspace
and build.
Usage of DataCollector SDK
Initialization
Import the library into your app:
Swift:
import TelenavSDKDataCollector
Objective-C:
@import TelenavSDKDataCollector;
Create SDK options with account credentials:
Swift:
swift
let options = TNDataCollectorSDKOptionsBuilder()
.apiKey("API_KEY_PROVIDED_BY_TELENAV")
.apiSecret("API_SECRET_PROVIDED_BY_TELENAV")
.cloudEndPoint("CLOUD_ENDPOINT_PROVIDED_BY_TELENAV")
.appInfo(name: "APPLICATION_NAME", version: "APPLICATION_VERSION")
.userId("USER_ID")
.deviceGuid("DEVICE_ID")
.build()
Objective-C:
```objc
TNDataCollectorSDKOptionsBuilder * builder = [TNDataCollectorSDKOptionsBuilder new];
[builder apiKey:@"API_KEY_PROVIDED_BY_TELENAV"];
[builder apiSecret:@"API_SECRET_PROVIDED_BY_TELENAV"];
[builder cloudEndPoint:@"CLOUD_ENDPOINT_PROVIDED_BY_TELENAV"];
[builder appInfoWithName:@"APPLICATION_NAME" version:@"APPLICATION_VERSION"];
[builder userId:@"USER_ID"];
[builder deviceGuid:@"DEVICE_ID"];
TNDataCollectorSDKOptions * options = [builder build];
```
Initialize Data Collector Service with options:
Swift:
TNDataCollectorService.initialize(sdkOptions: options)
Objective-C:
[TNDataCollectorService initializeWithSdkOptions:options];
Now you can get Data Collector Client from Service:
Swift:
let client = TNDataCollectorService.sharedClient
Objective-C:
TNDataCollectorClient * client = TNDataCollectorService.sharedClient;
Send events
Use Client method to send any supported event:
Swift:
func send(event: TNEvent)
Objective-C:
[client sendWithEvent: event];
This method returns an error (
TNDataCollectorError
) in case of event is not supported or Client was not properly configuredSupported events are:
TNFavoriteEvent TNRemoveAllFavoritesEvent TNEntityActionEvent TNEntityCacheActionEvent TNSetHomeEvent TNSetWorkEvent TNStartEngineEvent TNStopEngineEvent TNGpsProbeEvent TNGyroscopeEvent TNAccelerometerEvent TNLinearAccelerationEvent TNGravityEvent
Use apropriate builders in order to construct valid event before sending.
For example
TNGpsProbeEvent
:Swift:
let item = TNProbeListItemBuilder() .lat(55).lon(73) .locationTime(time) .altitude(20000.3) .horizontalAccuracy(5) .verticalAccuracy(6) .speed(200) .direction(.SW) .headingAngle(359) .build() let gpsEvent = TNGpsProbeEventBuilder().items([item]).build() TNDataCollectorService.sharedClient?.send(event:gpsEvent)
Objective-C:
TNProbeListItemBuilder * itemBuilder = [TNProbeListItemBuilder new]; [[itemBuilder lat:55] lon:73]; [itemBuilder locationTime:[NSDate new]]; [[itemBuilder altitude: 20000.3] verticalAccuracy:6]; [itemBuilder speed:200]; [itemBuilder horizontalAccuracy:5]; [itemBuilder direction:TNHeadingDirectionTypeSW]; [itemBuilder headingAngle:359]; TNProbeListItem * item = [itemBuilder build]; TNGpsProbeEventBuilder * eventBuilder = [TNGpsProbeEventBuilder new]; TNGpsProbeEvent * event = [[eventBuilder addItem:item] build]; [client sendWithEvent: event];
Subscribe to event type
Use Client method
subscribe
in order to subscribe to certain event type:Swift:
func subscribe(consumerWithName: String, forEventTypes: [TNEventType], withCallBack: (TNEvent)->())
Provide: - consumer name as an identifier for Data Collector, - the list of event types to subscribe to, - the callback to be executed when event of the type happens.
This method returns an error (
TNDataCollectorError
) in case of consumer name is empty or consumer already exists for the event type etc.For example:
Swift:
TNDataCollectorService.sharedClient?.subscribe(consumerWithName: "MyConsumerName", forEventTypes: [TNEventType.Sensor.linearAcceleration, TNEventType.User.setHome], withCallBack: { [weak self] (event) in print(event) })
Objective-C:
TNDataCollectorClient * client = TNDataCollectorService.sharedClient; NSArray<TNEventType *> * types = TNSensorEventType.values; [client subscribeWithConsumerWithName: @"MyConsumerName" forEventTypes:types withCallBack:^(TNEvent * _Nonnull event) { NSLog(@"%@: %@", [NSDate new], event.eventName); }];
You can use
values
property ofTNEventType
to subscribe for all kinds of some events: -TNEventType.Sensor.values
- for all events from sensors, -TNEventType.values
- for all possible supported eventsWhen needed use
unsubscribe
method of Client:Swift:
func unsubscribe(consumerWithName: String, forEventTypes: [TNEventType])
DataSource
Please read Readme for DataSource library