Watcher – A New App

It has been nearly a year since I last opened Xcode, Apple’s development environment for iOS and watchOS. One day, when I was rushing to an early morning class, I subconsciously patted my left pocket and realized that I had left my phone in my dorm room. I also took notice that I was wearing my Apple Watch, the device that I frequently use to ping my iPhone when I misplace it. While the watch does help me find my phone by allowing me to play a ringing sound on my phone, it doesn’t do so proactively. Wouldn’t it be nice to have an app on my Apple Watch that notifies me when the iPhone is out of range, I thought.

After I finished school, I ran back to my desk and opened Xcode for the first time in the year. Although I was very experienced in iOS programming, I had no idea how to create an app for Apple Watch. I did some research and decided that there are a couple of things I need to work on in order for the app to work as intended.

First, the app has to be able to send notifications to the user. Surprisingly, watchOS uses the UserNotifications framework which can also be found on (guess what) iOS. I happened to have worked on an iPhone app that sends notifications to the users using the same framework. Therefore, it did not take me long to send my very first notification to my Apple Watch (see featured image).

Next, the app has to be able to check its connection state with the paired iPhone. I went on Google and typed in “detect apple watch connection using swift.” From the first result, which was a Stack Overflow post, I learned that I had to use the Watch Connectivity framework to detect if the iPhone connected. I looked up the framework on Apple’s API reference website started testing. After nearly a week’s work, I successfully implemented the framework. The watch app now listens to changes in the connection state in the foreground and displays connection status accordingly.

Last but not least, the app must keep running in the background to notify the user. This might be a downer because I have not yet figured the best way to do this. For the purpose of power conservation, Apple does not allow apps to run continuously in the background. watchOS only allows apps to perform “Background App Refresh” for a short amount of time and for the following reasons:

  1. General purpose (happens about once per hour and thus does not meet the demand of this app)
  2. WatchConnectivity (triggered only when iPhone sends data to the watch)
  3. Snapshot (only takes a snapshot)
  4. URLSession (triggered when the watch needs to communicate with the Internet)

I have not yet attempted to use “Background App Refresh” on Apple Watch. Alternatively, I also have the option of using the “Background App Refresh” on iPhone which permits the usage of the Core Bluetooth framework to detect Apple Watch using Bluetooth.

Regardless of which method I use, there is still a lot of work for me to do. Hopefully, by the time you read my next blog, the app can be fully operational and ready to be released on the App Store.

Thanks for reading.

References

Apple. (n.d.). API Reference. Retrieved May 15, 2017, from Apple Developer website: https://developer.apple.com/reference

Tags: Swift [Online forum post]. (2017, May 15). Retrieved from Stack Overflow website: http:// stackoverflow.com/questions/tagged/swift

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s