{"id":911,"date":"2015-03-19T17:58:23","date_gmt":"2015-03-19T21:58:23","guid":{"rendered":"http:\/\/www.peteonsoftware.com\/?p=911"},"modified":"2024-03-02T15:59:24","modified_gmt":"2024-03-02T20:59:24","slug":"our-first-watchkit-app-watchcounter","status":"publish","type":"post","link":"https:\/\/www.peteonsoftware.com\/index.php\/2015\/03\/19\/our-first-watchkit-app-watchcounter\/","title":{"rendered":"Our First WatchKit App &#8211; WatchCounter"},"content":{"rendered":"<p>In my <a href=\"https:\/\/www.peteonsoftware.com\/index.php\/2015\/03\/16\/podcast-episode-35-the-apple-watch\/\">last podcast episode<\/a>, I talked about the Apple Watch Event and also a bit about what developing for the watch is like.  WatchKit isn&#8217;t something that you can just create as a standalone application.  Every WatchKit application must have a phone application.  No processing happens on the watch, it is just a &#8220;dumb screen&#8221; for the application on the phone.  So even if the entirety of what you are building is for the watch, the phone component must still be installed.  The phone component is the &#8220;brains of the operation&#8221;.<\/p>\n<p>Here is a diagram of WatchKit architecture from Apple<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/WatchKit_AppleDiagram.png\" alt=\"WatchKit Architecture from Apple, originally here: https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/General\/Conceptual\/WatchKitProgrammingGuide\/Art\/app_communication_2x.png\" title=\"WatchKit Architecture from Apple, originally here: https:\/\/developer.apple.com\/library\/prerelease\/ios\/documentation\/General\/Conceptual\/WatchKitProgrammingGuide\/Art\/app_communication_2x.png\" \/><\/p>\n<p>And, here is one that I created to expound on it a little bit:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/WatchKit_PeteDiagram.png\" alt=\"WatchKit Architecture\" title=\"WatchKit Architecture\" \/><\/p>\n<p>Let&#8217;s walk through making a very simple application.  The source for this application <a href=\"https:\/\/github.com\/PeteShearer\/WatchCounter\">is available on GitHub<\/a>, or you can follow along.<\/p>\n<p>First, we need to make a phone application.  You see that WatchKit isn&#8217;t any of our options, so I&#8217;m choosing to make a Single View application.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/01_NoFileNewWatchKit.png\" alt=\"You cannot File->New WatchKit&#8221; title=&#8221;You cannot File->New WatchKit&#8221; \/><\/p>\n<p>Next, I name it WatchCounter, make sure the language is Swift and the device is the iPhone and click Next.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/02_NameProject.png\" alt=\"Naming our Project in Xcode\" title=\"Naming our Project in Xcode\" \/><\/p>\n<p>At this point, we just have a standard iOS application and XCode appears as it always does.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/03_StandardXcode.png\" alt=\"Xcode before adding WatchKit\" title=\"Xcode before adding WatchKit\" \/><\/p>\n<p>With the project selected in the explorer pane, make sure that your Target window is visible.  If not, click this button to show it.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/04_TargetDrawer.png\" alt=\"Show Target Drawer in XCode\" title=\"Show Target Drawer in XCode\" \/><\/p>\n<p>Next, click the plus sign indicated here to bring up the Add Target window.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/05_AddTarget.png\" alt=\"Xcode Add Target\" title=\"Xcode Add Target\" \/><\/p>\n<p>We are going to choose an Apple Watch -> WatchKit App Target Template<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/06_WatchKitTarget.png\" alt=\"WatchKit Target\" title=\"WatchKit Target\" \/><\/p>\n<p>For the options, we are just making a simple application, so leave most of the defaults.  We are going to make sure that the checkboxes are cleared next to &#8220;Include Notification Scene&#8221; and &#8220;Include Glance Screen&#8221;.  If you forget, it won&#8217;t be the end of the world.  Our Watch Storyboard will just be a little cluttered.  Because we wouldn&#8217;t be using those screens for this sample, they wouldn&#8217;t affect what we&#8217;re doing right now.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/07_WatchKitTargetOptions.png\" alt=\"Xcode WatchKit Target Options\" title=\"Xcode WatchKit Target Options\" \/><\/p>\n<p>After you are done and click &#8220;Finish&#8221;, you are presented with this dialog (if you haven&#8217;t previously told it to never show again).  Click &#8220;Activate&#8221; so we can continue.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/08_ActivateWatchKitScheme.png\" alt=\"Activate WatchKit App scheme\" title=\"Activate WatchKit App scheme\" \/><\/p>\n<p>At this point, your project will be changed.  Added to our project are two new folders: WatchCounter WatchKit Extension, and WatchCounter WatchKit App.  For this project, the only thing we care about in the &#8220;App&#8221; folder is the Interface.storyboard and the only thing in the Extension folder we need is InterfaceController.swift.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/09_WatchKitProjectStructure.png\" alt=\"Our new project structure with the WatchKit pieces added\" title=\"Our new project structure with the WatchKit pieces added\" \/><\/p>\n<p>If we click the storyboard, we&#8217;ll see this:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/10_WatchKitStoryBoard.png\" alt=\"WatchKit Storyboard\" title=\"WatchKit Storyboard\" \/><\/p>\n<p>Go to the controls (bottom right of Xcode by default) and find the Button and Label controls.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/11_ButtonAndLabelControl.png\" alt=\"Button and Label Controls in Xcode\" title=\"Button and Label Controls in Xcode\" \/><\/p>\n<p>Drag the label onto the watch storyboard, followed by the button.  The watch has a &#8220;flow&#8221; layout and will just stack the controls on top of each other with no overlap.  You don&#8217;t have access to auto-layout or precise positioning.  There are ways to group some controls horizontally, but for now we can be fine with stacking the controls.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/12_ButtonAndLabelOnStoryBoard.png\" alt=\"Button and Label on the Storyboard\" title=\"Button and Label on the Storyboard\" \/><\/p>\n<p>Select the label on the storyboard and go over to the properties window and change the values to the ones that you see here.  The label&#8217;s text will change when the app starts up, so I just have some text in there so it is easy to see and select while we are working with it visually.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/13_SetLabelProperties.png\" alt=\"Setting the label's properties\" title=\"Setting the label's properties\" \/><\/p>\n<p>Next, select the button on the storyboard and then set its properties.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/14_SetButtonProperties.png\" alt=\"Setting the button's properties\" title=\"Setting the button's properties\" \/><\/p>\n<p>Now, if you click the assistant editor button while on the storyboard, it will bring up the code for the view controller side by side.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/AssistantEditorButton.png\" alt=\"Assistant Editor\" title=\"Assistant Editor\" \/><\/p>\n<p>Click on the label to select it and then hold down control, click on the label, and drag over to the code window and let go right above the awakeWithContext method.  A dialog will come up to create an outlet.  Name it displayLabel and click to accept.<\/p>\n<p>Next, control click the button and drag over to the code window right below the awakeWitContext method.  It will again ask you to create an IBOutlet, but use the dropdown to change to an IBAction, name it buttonWasPressed and accept.<\/p>\n<p>Next, type in the rest of the code shown here.  All we are doing is creating a counter variable and then updating the label, keeping track of how many times we clicked.  It should be fairly straightforward.<\/p>\n<pre>\r\nimport WatchKit\r\nimport Foundation\r\n\r\n\r\nclass InterfaceController: WKInterfaceController {\r\n    @IBOutlet var displayLabel: WKInterfaceLabel!\r\n    var counter = 0\r\n\r\n    override func awakeWithContext(context: AnyObject?) {\r\n        super.awakeWithContext(context)\r\n        \r\n        \/\/ Configure interface objects here.\r\n        updateMessage()\r\n    }\r\n\r\n    @IBAction func buttonWasPressed() {\r\n        counter++;\r\n        updateMessage()\r\n    }\r\n    \r\n    func updateMessage() {\r\n        var message = \"Pressed:\\(counter) time(s)\"\r\n        displayLabel.setText(message)\r\n    }\r\n}\r\n<\/pre>\n<p>Now, make sure that the WatchCounter WatchKit App is still the active schema and you are targeting the iPhone 6 simulator.<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/RunWatchKitApp.png\" alt=\"Run the application\" title=\"Run the application\" \/><\/p>\n<p>When you run it, should should launch the phone and watch simulator and get a watch app up that runs like this:<br \/>\n<img decoding=\"async\" src=\"https:\/\/www.peteonsoftware.com\/images\/201503\/15_WatchCounterDemo.gif\" alt=\"WatchCounter Demo\" title=\"WatchCounter Demo\" \/><\/p>\n<p>That&#8217;s it.  If you have any questions, let me know.  Again, if you want to check this out and poke around rather than follow along, the code for this application <a href=\"https:\/\/github.com\/PeteShearer\/WatchCounter\">is available on GitHub<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my last podcast episode, I talked about the Apple Watch Event and also a bit about what developing for the watch is like. WatchKit isn&#8217;t something that you can just create as a standalone &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75,84],"tags":[129,85],"class_list":["post-911","post","type-post","status-publish","format-standard","hentry","category-ios","category-watchkit","tag-ios","tag-watchkit"],"_links":{"self":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/911","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/comments?post=911"}],"version-history":[{"count":0,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/posts\/911\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/media?parent=911"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/categories?post=911"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.peteonsoftware.com\/index.php\/wp-json\/wp\/v2\/tags?post=911"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}