First pass on an Objective-C API wrapper

I did a little project with my Spark at Nodebots London yesterday. Unfortunately we ran into some mechanical issues (getting servos to move & trigger Nerf guns :wink: ), but I extracted some of the Objective-C code I used to build the client app.

I’m super new to Objective-C so this is probably terrible and useless, but hey! Would love suggestions on what else I should add - I’m going to work on adding the rest of the API methods today I think :smile:

2 Likes

Update - bumped the Podspec to 0.0.5; added some more methods for the rest of the API. Here’s the running total:

- (void)getDevicesUsingBlock:(void (^)(NSArray *responseObject))callback;

- (void)getDeviceUsingBlock:(void (^)(NSDictionary *responseObject))callback;

- (void)getDeviceWithDeviceID:(NSString *)deviceID usingBlock:(void (^)(NSDictionary *responseObject))callback;

- (void)runCommand:(NSString *)command args:(NSArray *)args usingBlock:(void (^)(NSDictionary *responseObject))callback;

- (void)getVariable:(NSString *)variable usingBlock:(void (^)(NSDictionary *responseObject))callback;
2 Likes

Awesome, thanks for sharing @jongold!

And I just submitted a little pull request to make the Spark Build code in the README work. :smile:

I’ve also been working on an Objective-C wrapper for the Spark API. I’ve taken the approach of creating a SparkCore object (a core) with deviceID and accessToken properties. You can create this object by passing your id and token if you want to hard code it into your app, or you can download those values from the cloud using your user name and password. If you have multiple cores, you can create the object using its name. These methods are,

+(instancetype)coreWithdeviceID:(NSString *) devID accessToken:(NSString *) accessToken;

+(instancetype)coreWithUserName:(NSString *) user password:(NSString *) password;

+(instancetype)coreNamed:(NSString *)coreName userName:(NSString *) user password:(NSString *) password;

+(instancetype)coreWithNewTokenNamed:(NSString *)coreName userName:(NSString *) user password:(NSString *) password;

Once you have the SparkCore object, you can access the Spark.variable() and Spark.function() API with block based methods. There are two different methods to call the Spark.function, one of which allows you to pass an array of keys that are used to create a dictionary of values gotten by parsing the 32 bit int returned by the function. This way, you can combine 2, 3, or 4 values (2 x 16 bit, 1 x 16 bit and 2 x 8 bit, or 4 x 8 bit) in your Spark code and have the method parse it into its components. Those methods are,

-(void) executeFunction:(NSString *)functionName argument:(NSString*) arg completionHandler:(void (^)(NSInteger result, NSError *error))handler

-(void) executeFunction:(NSString *)functionName argument:(NSString*) arg returnKeys:(NSArray *) keys completionHandler:(void (^)(NSDictionary *result, NSError *error))handler

There are four methods for accessing the Spark.variable, corresponding to the four possible return types of INT, DOUBLE, BOOLEAN, and STRING. This is probably overkill, but allows me to return the correct type for each one. These methods are,

-(void) readInt:(NSString *) variable completionHandler:(void (^)(int result, NSError *error))handler;

-(void) readBoolean:(NSString *) variable completionHandler:(void (^)(BOOL result, NSError *error))handler;

-(void) readDouble:(NSString *) variable completionHandler:(void (^)(double result, NSError *error))handler;

-(void) readString:(NSString *) variable completionHandler:(void (^)(NSString *result, NSError *error))handler;

The Xcode project, SparkCoreDemo, is up on GitHub here, https://github.com/edelmar/SparkCoreDemo.git

1 Like

@jongold I’m having trouble adding the API to my Podfile…
this is the error I’m receiving:

[!] Unable to find a specification for JGSparkKit

:frowning:

Hmm, weird - tbh I haven’t had time to play with my Spark for a few months; I think @Ric’s solution was actually a bit more elegant though - can you try that instead?

1 Like

Ok Thanks @jongold … !!