watchOS 2 WatchKit updates



There are many updates in WatchKit of watchOS 2. I will talk about five new enhancements. I found an issue about watchOS 2 beta 3 and beta 4. This issue is that only apps written in Objective C can run on a real Apple watch and apps written in Swift cannot run on a real device.

Animations

Now you can use animateWithDuration function of WKInterfaceController to animate some properties of objects.

 
[self animateWithDuration:0.3 animations:^{
   [self.buttonSpacerGroup setHeight:100];
}];

Taptic engine

Developers can manipulate Taptic engine on the Apple watch. The API is quite simple. Only one function (playHaptic) can be used, and just 9 types Taptic can be played.

WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Notification)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.DirectionUp)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.DirectionDown)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Success)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Failure)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Retry)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Start)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Stop)
WKInterfaceDevice.currentDevice().playHaptic(WKHapticType.Click)

WatchConnectivity

WatchConnectivity framework is used to communicate between iPhone and Apple Watch. The most important class is WCSession. The delegate is used to receive messages from a counterpart.

This is an example to send a message.

override func willActivate() {
    super.willActivate()
    
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

@IBAction func sendMessage(){
  let applicationDict = ["message":"test"]
  do {
      try session.updateApplicationContext(applicationDict)
  } catch {
      print("error")
  }
}

This is an example to receive a message.

override func viewDidLoad() {
    super.viewDidLoad()
    
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self;
        session.activateSession()
    }
}

func session(session: WCSession, didReceiveApplicationContext applicationContexString : AnyObject]) {
    let message = applicationContext["message"] as? String
    
    //Use this to update the UI instantaneously (otherwise, takes a little while)
    dispatch_async(dispatch_get_main_queue()) {
        self.label.text = "Last message: " + message!
    }
}

WKInterfacePicker

WKInterfacePicker is a new control in watchOS 2 and its value can be changed by spinning the digital crown. “focusForCrownInput” is the method to focus this control.

The below is the simplest example of WKInterfacePicker.

NSMutableArray *items = [[NSMutableArray alloc] init];
for(int c = 1; c <= 60; c++) {
    WKPickerItem *item = [[WKPickerItem alloc] init];
    item.title = [NSString stringWithFormat:@"No %d", c];
    [items addObject:item];
}
[self.picker setItems:items];

Complication

The important class of complication is CLKComplicationDataSource class. Developers need to create a class to implement this delegate. There are 5 families of complications: ModularSmall, ModularLarge, UtilitarianSmall, UtilitarianLarge, CircularSmall. Reference

This is the code to return current complication.

func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Voi
   if complication.family == .CircularSmall {
       let template = CLKComplicationTemplateCircularSmallRingText()
       template.textProvider = CLKSimpleTextProvider(text: "8")
       template.fillFraction = Float(getCurrentHealth()) / 10.0
       template.ringStyle = CLKComplicationRingStyle.Cl
       let timelineEntry = CLKComplicationTimelineEntry(date: NSDatcomplicationTemplate: template)
       handler(timelineEntry)
   } else {
       handler(nil)
   }
}