WatchKit 是 Apple 用于在 Apple Watch 上创建应用程序的框架,于 2014 年与 watchOS 1 一起发布。它提供了一套 WKInterface 界面元素——按钮、标签、表格、地图——以及屏幕之间的导航机制。尽管现代 watchOS 开发越来越多地转向 SwiftUI,但 WatchKit 对于支持现有应用程序和需要完全控制界面的场景仍然很重要。根据 Apple Developer Documentation, 2026,WatchKit 仍在 watchOS App Store 目录中 35% 的应用程序中使用,包括健身应用程序、导航器和快速数据访问工具。
要点
WatchKit — Apple 的框架,提供用于在 Apple Watch 上创建应用程序的 API。从 watchOS 2(2015)开始,WatchKit 应用程序直接在手表上运行,而不是在 iPhone 上,这使得它们无需与手机保持持续连接即可处理数据和显示界面。在此之前,watchOS 1 中的所有应用程序都在 iPhone 上运行,而手表只是一个远程显示遥控器,导致 UI 出现明显延迟。
现代 WatchKit 架构包括两个组件:WatchKit App(手表上的 Storyboard + 资源)和 WatchKit Extension(在手表上执行的代码)。用户在安装父级 iOS 应用程序时通过 App Store 安装这两个组件。WatchKit 提供与系统功能的集成:通知(通过 UNUserNotificationCenter)、复杂功能(表盘上的数据)、Workout API 和 HealthKit。
根据 Counterpoint Research (2025) 的数据,Apple Watch 占据全球智能手表市场 52% 的份额,普通用户使用 WatchKit 或 SwiftUI 安装 6–8 个第三方应用程序。最受欢迎的类别是健身(56%)、健康(22%)、导航(9%)和工具(8%)。
SwiftUI — Apple 推荐的从 watchOS 6 开始创建 watchOS 界面的方法。SwiftUI 提供声明式语法和自动适应屏幕尺寸的功能。然而,WatchKit 对于在 SwiftUI 出现之前启动的项目、使用特定元素(WKInterfaceMap、WKInterfaceMovie)以及需要支持低于 6 的 watchOS 版本的情况仍然具有现实意义。
| 标准 | WatchKit | SwiftUI |
|---|---|---|
| Apple 推荐 | Legacy | Current |
| 最低 watchOS | watchOS 1 | watchOS 6 |
| 代码量 | 更多 | 更少 |
| Compose 集成 | 通过类直接集成 | 通过 WidgetKit |
| WKInterfaceMap | 是 | Map(MapKit) |
WKInterfaceController — WatchKit 中管理屏幕的基类。负责生命周期:初始化(awake(withContext:))、出现(willActivate)、消失(didDeactivate)以及在控制器之间传递上下文。应用程序的每个屏幕都由一个单独的 WKInterfaceController 子类表示,通过 Interface Builder 与 Storyboard 关联。导航可以是层次式(push)或模态式(present)。
WatchKit 提供了一套带有 WKInterface 前缀的界面元素:WKInterfaceLabel(文本)、WKInterfaceButton(按钮)、WKInterfaceTable(表格)、WKInterfaceImage(图像)、WKInterfaceMap(地图)和 WKInterfaceGroup(具有圆角和背景的容器)。所有元素都异步工作——UI 更改被排队并在渲染周期之间由系统应用,确保手表屏幕上稳定的 30 FPS。
class MainInterfaceController: WKInterfaceController {
@IBOutlet weak var titleLabel: WKInterfaceLabel!
@IBOutlet weak var actionButton: WKInterfaceButton!
override func awake(with context: Any?) {
super.awake(with: context)
titleLabel.setText("你好,Watch!")
}
override func willActivate() {
super.willActivate()
actionButton.setTitle("开始")
}
@IBAction func didTapButton() {
pushController(withName: "DetailController", context: nil)
}
}
WCSession — 用于通过蓝牙或 Wi-Fi 在 Apple Watch 和 iPhone 之间进行双向通信的核心类。会话允许传输小型数据字典(updateApplicationContext)、发送带有即时响应的消息(sendMessage)、传输文件(transferFile)和同步复杂对象(transferUserInfo)。WCSession 异步工作并自动选择最佳通信通道。
要使用 WCSession,需要在两个设备上激活会话。Watch 应用程序在 willActivate() 方法中创建 WCSession,而 iOS 应用程序在 AppDelegate 或 SceneDelegate 中创建。会话激活后,设备会在可能时自动同步上下文。处理 WCSessionDelegate 委托以接收传入数据和跟踪连接状态非常重要。
class SessionManager: NSObject, WCSessionDelegate {
private let session = WCSession.default
func activate() {
session.delegate = self
session.activate()
}
func sendDataToPhone(key: String, value: Any) {
guard session.isReachable else { return }
session.sendMessage([key: value],
replyHandler: { response in
print("Response: \(response)")
},
errorHandler: { error in
print("Error: \(error.localizedDescription)")
}
)
}
func session(_ session: WCSession,
didReceiveMessage message: [String: Any],
replyHandler: @escaping ([String: Any]) -> Void) {
handleIncomingData(message)
replyHandler(["status": "ok"])
}
}
Complications 是显示在 Apple Watch 表盘上的小型数据元素,无需打开应用程序即可快速访问信息。WatchKit 提供 CLKComplicationDataSource——一个协议,其实现允许应用程序为复杂功能提供数据。表盘自行决定哪些位置可用——圆形(circular)、矩形(rectangular)、角落(corner)或模块化(modular)。
开发人员可以为三种尺寸系列提供复杂功能:CLKComplicationFamily — circularSmall、extraLarge、graphicCircular、graphicRectangular、graphicCorner、graphicBezel、modularSmall、modularLarge 和 utilitarianSmall/Large。每个系列都有自己的尺寸和显示格式。应用程序可以支持多个系列,但建议至少支持两个——图形和模块化。
class ComplicationController: NSObject, CLKComplicationDataSource {
func getCurrentTimelineEntry(
for complication: CLKComplication,
withHandler handler: @escaping (CLKComplicationTimelineEntry?) -> Void
) {
let template = CLKComplicationTemplateModularSmallSimpleText()
template.textProvider = CLKSimpleTextProvider(text: "96%")
let entry = CLKComplicationTimelineEntry(
date: Date(),
complicationTemplate: template
)
handler(entry)
}
}
创建 WatchKit 应用程序始于在 Xcode 中添加 WatchKit App Target。Xcode 会生成带有初始控制器的 Interface.storyboard,并自动将其与 InterfaceController 类关联。开发人员通过 Interface Builder 添加 UI 元素,并创建 IBOutlet 用于从代码进行交互。以下是一个完整的控制器示例,其中包含通过 WCSession 从 iPhone 接收的数据表格。
class ItemRowController: NSObject {
@IBOutlet weak var itemLabel: WKInterfaceLabel!
func configure(with text: String) {
itemLabel.setText(text)
}
}
class ListController: WKInterfaceController {
@IBOutlet weak var itemsTable: WKInterfaceTable!
private var items: [String] = []
override func awake(with context: Any?) {
super.awake(with: context)
items = context as? [String] ?? []
itemsTable.setNumberOfRows(items.count,
withRowType: "ItemRow")
for i in 0..if let row = itemsTable
.rowController(at: i) as? ItemRowController {
row.configure(with: items[i])
}
}
}
}
Workout API 是一组 WatchKit 类,用于创建可以在 Apple Watch 后台运行锻炼的健身应用程序。API 提供对传感器的访问:加速度计、陀螺仪、脉搏计(通过 HKHealthStore)和 GPS(在带 GPS 的 Watch 型号上)。锻炼通过 HealthKit 的 HKWorkoutSession 启动,脉搏数据通过 HKSampleQuery 实时更新。
Workout API 的关键优势是后台工作能力。当锻炼活动时,watchOS 不会在手臂放下时暂停应用程序——传感器继续收集数据,应用程序可以显示脉搏、距离、配速和其他指标。锻炼完成后,数据通过 HealthKit 与 iPhone 上的健康应用同步,确保用户的统一健身档案。
class WorkoutManager: NSObject {
private let healthStore = HKHealthStore()
private var session: HKWorkoutSession!
func startWorkout(activityType: HKWorkoutActivityType) {
let config = HKWorkoutConfiguration()
config.activityType = activityType
config.locationType = .outdoor
session = try! HKWorkoutSession(
healthStore: healthStore,
configuration: config
)
session.startActivity(with: Date())
}
func stopWorkout() {
session.stopActivity(with: Date())
session.end()
}
}
常见问题
Apple 建议新 watchOS 应用程序使用 SwiftUI。仅在支持遗留项目或需要 SwiftUI 中不可用的特定功能(如 WKInterfaceMap)时使用 WatchKit。
WCSession 通过 transferUserInfo 和 transferFile 支持延迟传输。数据将在手表和电话下次建立连接时送达,即使暂时失去连接也能保证送达。
使用 WatchKit 编写的应用程序兼容 Apple Watch Series 0 及更新版本(watchOS 1+)。但某些功能(GPS、脉搏计、动态岛)仅在某些型号上可用。
可以,您可以在一个应用程序中组合 SwiftUI 和 WatchKit。使用 WKHostingController 将 SwiftUI View 嵌入 WKInterfaceController,或反过来——通过 UIViewRepresentable 集成 WKInterfaceObject。
在连接的设备上从 Xcode 运行 两个应用程序(iOS + watchOS)。使用调试控制台观察 WCSessionDelegate 消息。确保两个设备都在蓝牙范围内(最远 10 米)。
总结
我们将开发一款交钥匙移动应用程序
IT Sectr自2017年以来为初创企业和企业打造iOS和Android应用程序。我们将为您提供咨询并提出最佳解决方案。