5 things you need to know before developing mobile applications
WatchKit has been available to developers for quite a while. As a developer, I finally finished my first Apple Watch app. I’d like to share some of my experience during the development.
Just like everyone else, I had so many questions at the very beginning, let’s start from these questions: What is WatchKit and how does it work? How many Apple Watches can I pair with one iPhone, how about iPad? How can a watch app communicate with its companion iPhone app? How notifications work on Watch app? Can I create custom watch UI layouts?
So, what is WatchKit and how does it work? Firstly, we have to be very clear that there is no Apple Watch project template in Xcode. Which means we can not create a new project for making an Apple Watch app just like creating a normal iOS project. Every Watch app requires a paired iPhone (the iPhone App as the containing app of the Watch App). Here is how it works: WatchKit is Apple’s framework for building hybrid apps for the Apple Watch, and it is bundled with Xcode 6.2. WatchKit works by splitting your app into two distinct parts: 1. Your iPhone App, which contains all of the code the developer write for the Watch app (Swift or objective-C, the code will run on the iPhone not on the Watch), and executes it as an extension; 2. Your Apple Watch, which contains just the View that the user will see on the Watch (like: the user interface resources: the storyboard and asset catalog, and even though it handles user input, but it doesn’t actually execute any of the code.)
Because all of the code runs on the iPhone App, the user don’t have access to the system on the Watch(like the heart rate monitor, like the user can access to the camera on the iPhone device, etc.) That’s why, there is no watch project, what we are doing is that we add Watch support to an iPhone project. So the Watch app will need a connected iPhone to run. The communication between the Apple Watch and the iPhone is automatic, and happens behind the scenes. The Apple Watch leverages both Bluetooth LE and Wi-Fi technologies to communicate with its paired iPhone. And now the user can only pair ONE Apple Watch with ONE iPhone at a time, can NOT pair the Apple Watch with an iPad.
About the notification, I have to say that it’s very easy to get the totally wrong idea about the notifications on Apple Watch. When we adding a new Watch App to the iPhone project, there is another choice: a checkbox to support a notification scene. But this checkbox does not mean what many people think it does. A lot of developers are under the impression that this is checked if notification is supported in your Watch app, and if it’s unchecked, you don’t support notifications in your Watch app. Actually, neither of those statement is true. The this is whether your Watch app receives or does not receive notifications has nothing to do with this checkbox.
Before you even consider a Watch app at all, the real question is that did you already implement notifications in your iPhone app? Because Watch App automatically extends existing notification behavior from the iPhone over to the Watch. If you’ve added support for either local notifications or remote notifications on the iphone, those existing iPhone notifications will begin automatically pushing over to the watch that you’re wearing, and you don’t have to do anything. It will just happen, and it really doesn’t matter if that checkbox was checked or unchecked, but the reverse is also true.
If your iPhone app does not currently use notifications, then checking that notification scene for the Watch App will not magically add any functionality at all. So NO notifications will happen, even if you checked that. If you want that behavior, you must add notification functionality to the iPhone app and configure everything there. It’s not added to the Watch app or the Watch extension. So people will be justly wondering, then what is the point of this checkbox? Well this checkbox is for a notification scene, and this is simply the option to customize how notifications appear on the Watch. This is just a customization option to change the default appearance.
Now, it’s time to talk about the WatchKit Architecture. First we need to talk about “Extension”. We begin an Xcode with a conventional iPhone project without Watch support, and that kind of project has two targets by default: The main iPhone app and the testing target for unit testing.
Then we need to add a new target for the Watch app. After add a WatchKit app to our project. So, we began with two targets, we add a new target, that equals three targets, right? Well, no. We now have four. We have a new WatchKit App, and we also have a new WatchKit Extension. And we’ll see both of WatchKit App and WatchKit Extension in X code. Both as targets and as separate folders in the project navigator(Please check the screen shot).
Here the thing is that we are adding Watch Support to an existing iPhone app.The WatchKit Extension is the part that stays on the iPhone. So the WatchKit Extension folder contains all of the Watch App code. All our behavior that stays on the phone and runs on the phone. And when the watch app is launched, by tapping the icon on the watch home screen, its corresponding Extension will automatically launch and run in the background on the paired iPhone to react to anything the watch app needs to do. And that’s why we have these two new parts in our project: the WatchKit Extension and the WatchKit App. And they’re two different targets because they’re installed in two different places.
So when one publishes an iOS app that includes Watch Support, and someone downloads that from the App Store, The full bundle downloaded from the App Store onto the iPhone will contain all the necessary pieces.The iPhone App, the WatchKit App, and the WatchKit Extension. It will install on the iPhone. If that iPhone has a paired Apple Watch you have the opportunity to install the WatchKit App. And it will push just the WatchKit App part containing just the storyboards and the resources over to the watch. If the user then launches that watch app, the WatchKit Extension will automatically be launched back on the iPhone. But you won’t see anything on the phone. If the phone was locked, it would stay locked. The iPhone could be sitting on your desk or in your pocket. But the Extension will run in the background to provide any initial data, or to react to events like button taps in the watch app.
In Part 2, we will create a real Photo Sharing Watch App by using this knowledge.