Making a publishEvent request from iOS

Let’s say I setup a very simple publish:

Spark.publish("Temperature", "23");

Normally via JavaScript I’d have to use the following code:

var eventSource = new EventSource("https://api.spark.io/v1/devices/" + deviceID + "/events/?access_token=" + accessToken);

eventSource.addEventListener('Temperature', function(e) {
    var parsedData = JSON.parse(e.data);
    // etc
}, false);

What if I wanted to do this in Objective-C or Swift? Is there some sort of wrapper that I can use?

Any help would be greatly appreciated.

The only resource I know of is the Spark iOS app. Perhaps you could look at that for some code examples: https://github.com/spark/ios-app

Javascript is very different to objectif-c or Swift, because of the event-driven nature. So a wrapper for Javascript in Objectif-C is not a real option.
If you want to port the library to IOS you simply need to open a HTTP Request and consume the Results. But keep in mind: you have to do the Asynchronus stuff by yourself.
The Eventsource (at a low level) is simply a pending HTTP Request, producing output if an event arrives.

There is a small Spark iOS library around.

2 Likes