How to work with WatchKit in Swift Language – iOS 8 #Tutorial #CounterWithWatch

Apple Watch represents a new chapter in the relationship people have with technology. Now you can deliver innovative new experiences to your customers on their wrist. And by adding WatchKit, you can take your apps even further by extending and enhancing their functionality on Apple Watch.

Lets learn how to work with WatchKit by an Counter Example.

Counter Watch Example
Counter Watch

Create New Project in XCode with Swift Language
New Project
New Project

Add Apple Watch Target in your Application
Add Target
Add Target

Select Apple Watch Target
Add Target Apple Watch
Add Target Apple Watch

Select Options for Target
Target Options
Target Options

Storyborad of watch
By adding target you will find the InterfaceController.swift and Interface.storyboard
Storyboard And Swift Files
Storyboard And Swift Files

Design watch with the controls
Design and reposition the controls with the properties of controls.
Design Controls in Watch
Design Controls in Watch

Now Time to do some code and connect the outlets
Connect Outlets
Connect Outlets

Code:
[code language=”obj-c”]
// InterfaceController.swift
import WatchKit
import Foundation
class InterfaceController: WKInterfaceController {
@IBOutlet weak var lblCounter: WKInterfaceLabel!
var counter:Int = 0
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
@IBAction func upButtonPressed() {
counter = counter + 1
lblCounter.setText("\(counter)")
}
@IBAction func downButtonPressed() {
counter = counter – 1
lblCounter.setText("\(counter)")
}
}
[/code]
Download project with this stuff AppleWatchCounterDemo.zip
Thanks!
Happy Coding 😀