Simple app for iPhone (Turn LED on/off)

I would like to make a simple app for my iPhone, where I can turn a LED on/off. I have made the simple example “Controlling LEDs over the Internet” and can control the LED using a terminal program.

I can’t figure out how to make the iPhone send API request??? Is there a method in Xcode I can use? or is there another way of doing it.

Hey there, you can look at the iOS-app source code for reference:

https://github.com/spark/ios-app :wink:

Thanks

Isn’t there a very very simple example out there, the Thinker app is a bit complex for my limited Xcode experience :wink:

Finally I solved the problem :frowning:

#define deviceID @"<your device id>"
#define token @"<your device token>"

if(theSwitch.isOn) //turn LED on
    {
        NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.spark.io/v1/devices/%@/led",deviceID]];
        NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL: url cachePolicy: NSURLRequestReloadIgnoringCacheData timeoutInterval: 60.0];
        [postRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [postRequest setHTTPMethod:@"POST"];
    
    NSString *command = @"l1,HIGH";
    NSString *bodyData = [NSString stringWithFormat:@"access_token=%@&params=%@",token,command];
    [postRequest setHTTPBody:[NSData dataWithBytes:[bodyData UTF8String] length:strlen([bodyData UTF8String])]];
    
    [NSURLConnection sendAsynchronousRequest:postRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError)
     {
         if (!connectionError)
         {
           
         }
         else
         {
           
         }
     }];    
}
4 Likes

I made a VERY simple app, for the CONTROL LEDS OVER THE 'NET example, but I don’t know where to share it :slight_smile: If someone wanted to play with it…

Now on GitHub :slight_smile: Hope that someone will find it useful…

1 Like

It might be useful to add a link to the relevant GitHub page. That place is rather big :wink:

1 Like

Yeah please post the Github link because I’m starting iOS development myself and that would be a huge leap forward :smiley: and a good chance to give back to the community!

I made an App to work with my project, shared here: http://community.spark.io/t/sparkcore-microview-https-post-interaction/6425
Scroll to the bottom of the first post, and you can find a dropbox link to an Xcode project archive. You will need to edit the XYZViewController.h file with your device ID and Access code.

Let me know if this is useful

Uhh sorry :slight_smile:

Remember it’s only a proof of concept project, I haven’t done anything to the GUI and the two button event handlers do the same, only the params are different. I did this on purpose, so the beginner didn’t got lost in fancy functionality

1 Like

@kelle also wanted to include a quick reminder that if your goal is simply to toggle an LED on and off over the net, you can always use the default Tinker app as an easy stand-in as a remote control :smile:

1 Like

I know but I want to make my own app and learn how to do it.

Where is the fun in just using the tinker app :wink:

1 Like

I wrote a library to assist in iOS development for the Spark. It also has an example of turning an LED on/off: https://github.com/hoffmanjon/SparkCoreIOS

1 Like