Swift App example on GitHub

I’ve put up an example Swift App and the corresponding Core/Photon firmware on GitHub. My username is BendrixL. It has been forked and vetted by another forum member so I’m reasonably sure it will work for you. To use the app you should have the latest version of Xcode/Swift. Follow the instructions posted here to install cocoa pods on your Mac.

You only need to take those instructions as far as the cocoa pods install on your Mac. The pods install to the project is all done and will come down with the fork from GitHub.

This app uses your username and password to log into Particle, and then your device name to access your core/photon. The app reads two variables and activates one function. Each operation is under the control of a single button on the UI. The code is reasonably well documented and as far as the access to the Mobile SDK, it is line for line what @ido posted on the help page.

You can run it as is, with the corresponding firmware (also on GitHub), or change the names of the variables and function to match something you’ve got running on your device.

If you have questions, problems, or suggestions, please contact me here.

The data displayed in the example is raw sensor data. I removed the math I used as I would not know what your circuit might be. Add some math to make sense of the sensor data.

Challenge: After you look at the code, you might say, wouldn’t it be nice to update the variable each time I run the function. You might consider cutting and pasting the variable code into the class with the function call. The problem is that you won’t get the current reading, you’ll be one reading behind because the function will not have run before the variable is accessed. The two closures are asynchronous. Here is the challenge. Add code to read the variable after the function is run but force the variable read to wait until the function returns. I’ve been poking around Stack Overflow and other sources but not been able to come up with something that works. Lots of people have made suggestions, but nothing yet has worked. Good luck!


@Bendrix thank you for building and sharing this app. It worked on the first run. Very cool stuff.

Also, thanks for the detailed installation guide. This being my first time playing with iOS, I needed a guide like yours.


1 Like

This helped me so much! Thanks for saving me from many more hours of frustration.