Apple Watch & Mobile SDK - Do something cool with your wrist!


#1

If you would like to use the Particle SDK, written by @ido, with an Apple Watch, using an app written in Swift, here are the instructions you will need in order to get the Mobile SDK operating with the WatchKit Extension. With these instructions you will be able to do in a few minutes what took me a looooong time to figure out.

OK, here goes…

I recommend you begin with a fully operational project that has the Particle Mobile SDK working, and deployed, on a phone.

If you are unsure how to add a watchkit to your iOS project, I recommend Ray Wenderlich’s tutorial for the WatchKit with Swift. Find it at www.raywenderlich.com

After you have added the WatchKit Target to your working IOS project, if you start adding code before making the following configuration changes, you’ll get something like this.

No fun. Can’t deploy that!

Quit Xcode, launch terminal and navigate to the project directory that contains your project.

Do a pod update.

This should take care of everything… But no.

Relaunch your project using the xcworkspace file, just as you must with any pod dependency managed project.

Click on the blue main folder for the project. Click on Info. You will see the main project plus four targets below it.

See Configurations in bold. Under Configurations are two entries, Debug and Release. Beside them under Based on Configuration File you will see 1Configuration set.

Expand Debug and Release by clicking on the triangle.

Note in the screenshot above that the Debug WatchKit Extension line is highlighted and I have already added the choice Pods.debug to it. You do that by clicking on the word None, then choosing Pods.debug.

In the WatchKit Extension line under Release, click on None and choose Pods.release

Now your screen should look like this.

Now look at the list of Targets under the main project. Note the Target named WatchKit Extension. Click on that target. Now, at the top of the page, click on the General tab. You should see a screen like this.

Look at the section named Linked Frameworks and Libraries. There are no entries. For curiosities sake, click on the Target for the iPhone app; the first one in the list. You will see there is an entry here, for libPods.a. Go back to the WatchKit Extension target and where you see the + sign at the bottom of the Linked Frameworks and Libraries section, click on it. You will now see this screen. Choose libPods.a

When you have successfully added libPods.a to the WatchKit Target Linked Frameworks and Libraries your screen will look like this

OK! Those are the entries that pod update did not make for you. When you create a base project (iPhone) and do the steps explained in my other posts, the correct entries are made for the base project settings. However, cocoa pods is not currently making the correct settings when a WatchKit Target is present.

Whoopeee! Now you can use the cool Mobile SDK to create a Watch app by adding code to the WatchKit Extension InterfaceController.swift file, as shown below.

There are a few challenges to be surmounted with the Watch. Foremost is that there is no room nor a keyboard to enter login information. You will have to take care of that in your main app, and place it in a shared storage such as NSUserDefaults, or Core Data. That is if you want to deploy the app and make the login information configurable.

If you are just making demos or your own ‘Maker’ project, you will have to hard code the login and device information (as I did above), if you don’t want to create the shared data store.

The Watch apps run entirely on the iPhone (today), but they run in their own ‘sandbox’. There is no communication allowed between Watch apps and Phone apps (other than shared data). What does that mean for Watch App creation? Simply that you have to duplicate the code for login, device capture and function call/ variable read for the watch app, in InterfaceController.swift.

I’ll be doing a complete Watch app, with shared data store, and putting it on GitHub. I’ll post that when its up. Have fun! It sure is fun for me to show people my watch, touch it, and explain what just happened back at my office, and read a variable to prove it. Hope to see @zach geeking out with one of these soon!


#2

Hi Bendrix, I am trying to follow your instructions here, but unfortunately without success. I have managed to get the initial SDK setup done (with the help of your other post as well as with the youtube video by HackAttack on the same subject, see here https://www.youtube.com/watch?v=_aotVQGhPH8). With that I was able to run a couple of example codes from the particle website, e.g. login, list devices, read a variable.

However, I do not succeed in implementing your instructions here for the watch kit. I can follow the first part, but when it comes to the second part, the one where you talk about the " Linked Frameworks and Libraries" sections, there is no “libPods.a” in the list to select. In your example, you basically add the same entry that is there already for the iPhone app. But when I go to the Target for the iPhone app in order to see what entry is there, and I then select the same entry for the watch kit extension, it does not compile. In the “Issue navigator” there is one in red (i guess this is the most significant one) and it is one of those that I can also see in your first screen shot: “Apple Mach-O Linker Error - Linker command failed with exit code 1 - use -v to see vocation”. Would you have any idea what is wrong here? Are my selection options maybe different because the Xcode version has changed in the meantime too (now version 9)? Any help would be much appreciated, thanks in advance.