Skip to content

Instantly share code, notes, and snippets.

@paulkaplan
Last active August 29, 2015 14:03
Show Gist options
  • Save paulkaplan/9c4609b9790763ceadbf to your computer and use it in GitHub Desktop.
Save paulkaplan/9c4609b9790763ceadbf to your computer and use it in GitHub Desktop.
Getting started with remote function calls on Spark Core
- (void) makeSparkRequest {
NSString *sparkApi = @"https://api.spark.io/v1/devices";
NSString *deviceId = @"<YOUR_DEVICE_ID>";
NSString *functionName = @"<YOUR_FUNCTION_NAME>"; // registered to cloud using Spark.function("FUNCTION_NAME", FUNCTION);
NSString *accessToken = @"<YOUR_ACCESS_TOKEN>";
NSURL *url = [NSURL URLWithString: [NSString stringWithFormat:@"%@/%@/%@\?access_token=%@",
sparkApi,
deviceId,
functionName,
accessToken]
];
NSLog(@"%@", url);
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
NSError *error;
NSURLResponse *response;
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
// ... do something with returned data
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment