Apple Watch
Mobile Application Development in iOS School of EECS Washington State University
Mobile Application Development in iOS 1
Apple Watch Mobile Application Development in iOS School of EECS - - PowerPoint PPT Presentation
Apple Watch Mobile Application Development in iOS School of EECS Washington State University Mobile Application Development in iOS 1 Outline Xcode configuration WatchKit Notifications Complications Sensors Mobile
Mobile Application Development in iOS School of EECS Washington State University
Mobile Application Development in iOS 1
Mobile Application Development in iOS 2
Mobile Application Development in iOS 3
New in watchOS 6 (released Sep 2019): Independent watchOS apps
Mobile Application Development in iOS 4
File à Newà Target
– Storyboard – Storyboard assets
– App code
Mobile Application Development in iOS 5
Mobile Application Development in iOS 6
Mobile Application Development in iOS 7
Mobile Application Development in iOS 8
Mobile Application Development in iOS 9
import WatchKit import Foundation class InterfaceController: WKInterfaceController { @IBAction func pageTwoManualTapped() { pushController(withName: "Page Two", context: "Hello Manually") }
if segueIdentifier == "toPageTwoAuto" { return "Hello Automatically" } return nil } }
InterfaceController.swift
Mobile Application Development in iOS 10
import WatchKit import Foundation class PageTwoInterfaceController: WKInterfaceController { @IBOutlet weak var messageLabel: WKInterfaceLabel!
super.awake(withContext: context) // Configure interface objects here. if let message = context as? String { messageLabel.setText(message) } } }
PageTwoInterfaceController.swift
Mobile Application Development in iOS 11
Mobile Application Development in iOS 12
@IBAction func alertTapped() { let action1 = WKAlertAction(title: "Yes", style: .default, handler: {print("Yes")}) let action2 = WKAlertAction(title: "No", style: .destructive, handler: {print("No")}) presentAlert(withTitle: "Alert", message: "Are you okay?", preferredStyle: .sideBySideButtonsAlert, actions: [action2,action1]) }
Mobile Application Development in iOS 13
– Call requestAuthorization on watch – Handle notifications using didReceive and willPresent – Schedule UNNotificationRequest using UNUserNotificationCenter.add – Remote and background notifications can now be sent directly to watch
Long Look
Mobile Application Development in iOS 14
Short Look Long Look
Mobile Application Development in iOS 15
import WatchKit import UserNotifications class ExtensionDelegate: NSObject, WKExtensionDelegate { func applicationDidFinishLaunching() { // Perform any final initialization of your application. let center = UNUserNotificationCenter.current() center.requestAuthorization(options: [.alert]) { (granted, error) in if granted { print("notifications allowed") } else { print("notifications not allowed") } } } }
ExtensionDelegate.swift
Mobile Application Development in iOS 16
– Simple elements (fast)
– Outlets in NotificationController – No interactive elements – If takes too long, uses Static
– Allows interactive elements – Outlets/Actions in NotificationController
Mobile Application Development in iOS 17
Interface Load Order: (1) Dynamic Interactive, (2) Dynamic, (3) Static
– Make sure Notification Category Name is empty – Or, make sure notifications have same category identifier
Mobile Application Development in iOS 18
Mobile Application Development in iOS 19
Mobile Application Development in iOS 20 import UserNotifications func scheduleNotification() { // Same as in Notifications lecture notes let content = UNMutableNotificationContent() content.title = "Hey!" content.body = "What’s up?" content.userInfo["message"] = "Yo!" // If Notification Category set in Storyboard, then set here too // content.categoryIdentifier = "myCategory" // Configure trigger for 5 seconds from now let trigger = UNTimeIntervalNotificationTrigger(timeInterval: 5.0, repeats: false) // Create request let request = UNNotificationRequest(identifier: "NowPlusFive", content: content, trigger: trigger) // Schedule request let center = UNUserNotificationCenter.current() center.add(request, withCompletionHandler: { (error) in if let err = error { print(err.localizedDescription) } }) }
InterfaceController.swift
Mobile Application Development in iOS 21
import WatchKit import Foundation import UserNotifications class NotificationController: WKUserNotificationInterfaceController { @IBOutlet var dynamicInteractiveLabel: WKInterfaceLabel!
let title = notification.request.content.title dynamicInteractiveLabel.setText(title) } }
NotificationController.swift
Mobile Application Development in iOS 22
complication data (e.g., appointments)
guidelines/watchos/app-architecture/complications
Mobile Application Development in iOS 23
– getSupportedTimeTravelDirections(complication, handler)
– getCurrentTimelineEntry(complication, handler)
Mobile Application Development in iOS 24
– Create a CLKComplicationTemplate, e.g.,
– Create and set providers for template, e.g.,
– Create time line entry for template at date
– Send entry to handler
Mobile Application Development in iOS 25
Mobile Application Development in iOS 26
import ClockKit class ComplicationController: NSObject, CLKComplicationDataSource { func getSupportedTimeTravelDirections(for complication: CLKComplication, withHandler handler: @escaping(CLKComplicationTimeTravelDirections)
handler([.forward, .backward]) // or .forward, or .backward, or [] } }
Mobile Application Development in iOS 27
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void) { if (complication.family == .graphicCircular) { // Construct template with image, open gauge, and text let template = CLKComplicationTemplateGraphicCircularImage() let image = UIImage(named: "Complication/Graphic Circular") template.imageProvider = CLKFullColorImageProvider(fullColorImage: image!) // Create the timeline entry. let entry = CLKComplicationTimelineEntry(date: Date(), complicationTemplate: template) handler(entry) } else { handler(nil) } }
Mobile Application Development in iOS 28
(1) Configure Complications (2) Choose Complication scheme
Mobile Application Development in iOS 29
(3) Customize clock face Note: Shift-Command-1/2 to set shallow/deep press in simulator. Goto clock face Deep Press Customize Swipe to end Goto clock face Rotate crown to select
– Accelerometer – Gyroscope
– GPS
– Heart rate
Mobile Application Development in iOS 30
Mobile Application Development in iOS 31
– developer.apple.com/documentation/watchkit – Notifications
– developer.apple.com/documentation/clockkit – Complications
– developer.apple.com/documentation/healthkit
Mobile Application Development in iOS 32