The "Hello ContextHub" sample app that first introduces you to power the ContextHub iOS SDK and developer portal
- Purpose
- ContextHub
- Getting Started
- Xcode Console
- Developer Portal
- Context Rules
- Creating a New Rule
- Using the Vault Object
- Final Words
Today's world is full of devices and sensors just waiting to be tapped into. Meaningful events happen all the time, when we get home from work, approach an interesting painting, or visit our favorite coffee shop. ContextHub takes those meaningful events in our lives and transforms them into powerful triggers your apps can respond to turn apps into experiences for your end users.
In this sample application, we use ContextHub introduce the relationship between devices triggering events on the server by setting up geofences on a map. ContextHub takes care of setting up and monitoring geofences to generate events that the server can listen and respond to and then save those events in a vault to be accessed later.
- Get started by either forking or cloning the Hello ContextHub repo. Visit GitHub Help if you need help.
- Go to ContextHub and find the app id associated with the "Hello World" app. Its format looks something like this:
13e7e6b4-9f33-4e97-b11c-79ed1470fc1d
. - Open up your Xcode project and put the app id into the
[ContextHub registerWithAppId:]
method call. - Build and run the project in the simulator.
- This sample demo will log events into the debug console to get an idea of the JSON structure posted to ContextHub. Use shortcut
Shift-⌘-Y
if your console is not already visible. - When the app is launched, it should generate a
"location_changed"
event in the console which you can look at. You are able to access information about the event like device name, device ID, latitude, longitude, and speed. - Within the application delegate are 4 methods defined by the
CCHSensorPipelineDataSource
andCCHSensorPipelineDelegate
which allow you hook into the event pipeline of events generated by ContextHub. You can get notified when an event will post and has been posted, as well as control if an event should be posted and add extra payload data to an event. These 4 methods allow a lot of flexibility in controlling what events get sent to the server for additional processing. - Check out the ContextHub documentation for more information about the event pipeline and what you can do with it.
- The real power of ContextHub comes from collecting and reacting to events posted from devices onto the server. Go back to the developer portal and click on your app "Hello ContextHub" to access its data.
- You are brought to the Application home page which will show you the latest events generate from your devices.
- Click on the Contexts link. This will take you to the "Contexts" page which controls how the ContextHub server responds to events generated from devices. To the left you'll see "Contexts", which are rules which allow actions to be triggered depending on event data. To the right, you'll see your latest events triggered by devices with your app installed.
Context rules let you change how the server will respond to events triggered by devices. Let's go ahead and create a new rule that will save the some data on the server in the vault.
- In the "Contexts" tab, click the "New Context" button to start making a new rule.
- Enter a name for this context which will be easy for you to remember. For now, name it "Geofence in".
- Change the event type to
"geofence_in"
. Now any event of type"geofence_in"
will trigger this rule. You can have multiple rules with the same event type, which is why name of events should be descriptive of the rule. You can also have custom event types which you can trigger yourself inside your apps. - Below is where you can write a rule telling ContextHub what actions to take in response to an event triggered with a specific event type. This code is Javascript, and you have access to 4 objects: event, push, vault, and console.
- Back in XCode, stop and restart the simulator.
- Simulate a location change in the device to one of the major cities XCode provides (click on the compass arrow in the debug controls area of XCode in the bottom). Zoom in as closely as possible to street level where the location circle appears on the map.
- Tap the "Create Geofence" button to see a geofence appear on the map. You should also see a Fence creation event in the XCode console. If the simulated location is outside of the geofence circle, move the map around until the circle is centered on the screen and try again.
- Change the simulated location again in XCode to be another major city. You should see a
"geofence_out"
event posted in the console. If you are having issues with events not being generated, make sure you have network connectivity so you can get back responses from ContextHub. - Change the simulated location in XCode back to where you previously were and you should see a
"geofence_in"
event trigger as well. - In the ContextHub developer portal, you should be able to see events generated by your device appear under "Latest events", no code needed!
Now that we've gotten the app to trigger events that both appear on the device and the server, it's time to create a rule that does something with that event.
- Go back to "Contexts" and click on the
"location_changed"
context. - This page is where you previously set the rule related to
"geofence_in"
to"Always execute"
. Instead, let's save the event data to the vault using Javascript. - Like stated previously, context rules have access to 3 objects: events, push, and vault. Events contain data about the event which triggered the rule. Push lets you send push notifications to devices. And the vault object lets you persist data on the server for retrieval later. Take a look at the vault object by clicking on it and seeing what methods it has access to.
- When creating an object in the vault, it's best practice to tag it. You can use tags to hold similar vault items in a group for ease of access. To save the event object with a tag called "sample", type
vault.create(JSON.stringify(event), "sample")
into the Code editor then click save. - Restart the simulator again, check and see if a
"location_changed"
event was generated, then go back to the developer portal to see if the event appeared in "Latest Events" under "Contexts". - Now click on "Vault" at the top to see if a "sample" container with an item with event data was created. Each time the
"location_changed"
event is received by ContextHub, it will create a new item in the vault for you to access based on the rule you just created. - That's all it takes to create a rule and put data into the vault!
Hopefully you found the ease of use and power of our platform a compelling reason to use ContextHub an integrate it into your own projects. There are several more sample applications that go into specific detail around using geofences, beacons, push, and contextual rules to create powerful contextual applications!