Apple iWatch Programming : Learning watchOS 2

As you know last  year Apple introduced watchOS 2, which let you make native apps that run directly on your watch. So i was thinking we can also start Apple iWatch Programming and start Learning watchOS 2.

Getting Started

Start new project in Xcode. Select watchOS application from left menu.

Apple iWatch Tutorial watch OS 2

 

 

On clicking next give a name to your app.  You will see different options like “Include Notification Screen” , “Include Glance Screen” & “Include Complication“.

Apple iWatch Tutorial watch OS 2

Clicking on “Include Notification Screen” adds a NotificationController class to your project. Its for displaying your notifications. Its a view controller not a UIViewController but WKUserNotificationInterfaceController. Use to display both local and remote notifications.

Clicking on “Include Glance Screen” adds a GlanceController class to your project. You may use it as nonscrollable screen that summarizes the current state an application is in. Glances are non-interactive and they shouldn’t contain controls, such as buttons and sliders. If a user taps on a Glance, the corresponding Watch app is launched

Clicking on “Include Complication”  box adds a ComplicationController class to your project and configures the appropriate bits. When you go to view your extension target in Xcode, you’ll see checkboxes for the five potential classes of complication

 

WatchKit framework

There are no so much in this just a handful of classes listed below. All using these we will build best app for our iWatch 🙂

WKInterfaceController

WKInterfaceController in WatchKit is the same as UIViewController in UIKit. WKInterfaceController has a different life cycle form UIViewController. We cannot use our standard Cocoa classes.

awakeWithContext:

This is the designated initialiser for WKInterfaceController. The context parameter is used in order to pass any data from a previous interface controller to this one.

willActivate and didDeactivate

These are the methods you’d override in order to get notified whenever an interface controller becomes visible or is being dismissed. willActivate is the place you will need to make sure your controller’s information is up to date and you can use didDeactivate in order to clean up.

WKUserNotificationInterfaceController

WKUserNotificationInterfaceController is a WKInterfaceController subclass that is used to display custom notification interfaces.

WKInterfaceDevice

This includes screen bounds, scale and current locale, just some basic information related to the iWatch. You can also use it to manage your images cache.

WKInterfaceObject

The base class for all UI controls of WatchKit. We cannot use UIViews from UIKit. But it has its owns WatchKit objects:

  • WKInterfaceTable
  • WKInterfaceButton
  • WKInterfaceGroup
  • WKInterfaceImage
  • WKInterfaceLabel
  • WKInterfaceDate
  • WKInterfaceMap
  • WKInterfaceSeparator
  • WKInterfaceSlider
  • WKInterfaceSwitch
  • WKInterfaceTimer

WatchKit layout

WatchKit does not use Autolayout to position it’s element. We positions elements relative to each other in a horizontal or vertical order. We an change all this in Interface builder via the Attributes inspector.

 

Apple iWatch Tutorial watch OS 2

 

Doing Some Coding

Drag drop a label on Interface Controller. Change its text. Now run your app.Apple iWatch Tutorial watch OS 2

 

Yay you did it. Congrats.

Our todays tutorial on Apple iWatch Programming : Learning watchOS 2  contains basic of iWatch Programming. In our next tutorial we will do deeper and build something exciting. Till then Happy Coding 😉

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.