创建 watchOS 应用
复制一个包含安装步骤和此插件的完整Markdown指南的设置提示。
本指南将指导您从零开始创建一个 watchOS 陪伴应用,包括在 Xcode 中设置项目、集成 CapgoWatchSDK 和使用 SwiftUI 构建一个功能性的 watch 应用。
先决条件
标题:先决条件开始之前,请确保您有:
- Xcode 15 或更高版本 (从 Mac App Store 下载)
- macOS Sonoma 或更高版本 (用于最新的 watchOS SDK)
- 一个现有的 Capacitor iOS 项目 (运行)
npx cap add ios如果您还没有) - Apple Developer账户 (免费账户适用于开发)
项目结构概览
项目结构概览完成本指南后,您的项目将具有以下结构:
目录ios/
目录App/
目录App/ 主 iOS 应用
- …
- App.xcodeproj
- App.xcworkspace 使用此文件打开项目
- Podfile
目录MyWatch/ 目录
MyWatch/手表应用源码 MyWatchApp.swift
- __CAPGO_KEEP_0__
- ContentView.swift
目录Assets.xcassets/
- …
- MyWatch.xcodeproj
步骤 1:在 Xcode 中打开您的 iOS 项目
标题为“步骤 1:在 Xcode 中打开您的 iOS 项目”- Navigate to your Capacitor project’s
ios/App文件夹 - 打开
App.xcworkspace(不是.xcodeproj)通过双击它 - 等待 Xcode 索引项目
步骤 2:添加 watchOS 目标
标题:步骤 2:添加 watchOS 目标-
在 Xcode 中,转到 文件 → 新建 → 目标…
-
在模板选择器中:
- 选择 watchOS 顶部标签
- 选择 应用
- 点击 下一步
-
配置您的智能手表应用:
- 产品名称:
MyWatch(或您的首选名称) - 团队: 选择您的Apple开发者团队
- 组织标识符: 应该与您的iOS应用程序匹配(例如,
app.capgo) - Bundle Identifier: 将会自动生成(例如,
app.capgo.myapp.watchkitapp) - Language: Swift
- User Interface: SwiftUI
- Watch App Type: App (不适用于现有 iOS App)
- Uncheck Include Notification Scene (除非您需要)
- Uncheck 包含复杂性 (除非你需要)
- 产品名称:
-
点击 完成
-
当提示“激活 ‘MyWatch’ 方案?”时,点击 激活
步骤 3:配置手表应用设置
步骤 3:配置手表应用设置-
在项目导航器(左侧边栏)中,选择您的项目(顶部蓝色图标)
-
从目标列表中选择您的手表目标(例如,“MyWatch”)
-
转到 一般 标签:
- 显示名称: 应用程序图标下显示的名称(例如,“我的应用程序”)
- 包标识符: 应该以
.watchkitapp - 版本: 匹配 iOS 应用程序版本
- 构建: 匹配 iOS 应用程序构建号
-
前往 签名 & 能力 标签:
- 启用 自动管理签名
- 选择您的 团队
- Xcode将自动创建配置文件
-
设置 部署信息:
- 最小部署: watchOS 9.0 或更高版本
步骤 4: 通过 Swift Package Manager 添加 CapgoWatchSDK
标题:步骤 4:通过 Swift Package Manager 添加 CapgoWatchSDKCapgoWatchSDK 提供一个可用的解决方案 WatchConnector 用于通信的类。
-
在 Xcode 中,前往 文件 → 添加包依赖项…
-
在搜索字段中输入:
https://github.com/Cap-go/capacitor-watch.git -
按 Enter 并等待 Xcode 获取包
-
配置包:
- 依赖规则: '到下一个主要版本','8.0.0'
- 点击 添加包
-
选择要添加的产品:
- 重要: 只选择
CapgoWatchSDK - 确保它添加到你的 watch目标 (例如,“MyWatch”),而不是 iOS 应用
- 点击 添加包
- 重要: 只选择
步骤 5:实现 Watch 应用
步骤 5:实现 Watch 应用现在让我们创建 code。将自动生成的文件替换为以下内容:
5.1 创建应用入口点
5.1 创建应用入口点编辑 MyWatch/MyWatchApp.swift:
import SwiftUIimport CapgoWatchSDK
@mainstruct MyWatchApp: App { init() { // Activate WatchConnectivity when app launches WatchConnector.shared.activate() }
var body: some Scene { WindowGroup { ContentView() } }}5.2 创建主视图
5.2 创建主视图编辑 MyWatch/ContentView.swift:
import SwiftUIimport CapgoWatchSDK
struct ContentView: View { // Observe the WatchConnector for automatic UI updates @ObservedObject var connector = WatchConnector.shared
// Local state @State private var messageText = "" @State private var statusMessage = "Ready"
var body: some View { ScrollView { VStack(spacing: 16) { // Connection Status ConnectionStatusView(connector: connector)
Divider()
// Message Input TextField("Message", text: $messageText) .textFieldStyle(.roundedBorder)
// Send Buttons HStack { Button("Send") { sendMessage() } .disabled(!connector.isReachable || messageText.isEmpty)
Button("Request") { sendWithReply() } .disabled(!connector.isReachable || messageText.isEmpty) }
Divider()
// Status Text(statusMessage) .font(.caption) .foregroundColor(.secondary)
// Last Received Message if !connector.lastMessage.isEmpty { VStack(alignment: .leading) { Text("Last Message:") .font(.caption) .foregroundColor(.secondary) Text(formatMessage(connector.lastMessage)) .font(.caption2) } .frame(maxWidth: .infinity, alignment: .leading) } } .padding() } }
private func sendMessage() { connector.sendMessage(["text": messageText, "timestamp": Date().timeIntervalSince1970]) statusMessage = "Message sent" messageText = "" }
private func sendWithReply() { connector.sendMessage(["text": messageText, "needsReply": true]) { reply in DispatchQueue.main.async { statusMessage = "Reply: \(formatMessage(reply))" } } messageText = "" }
private func formatMessage(_ message: [String: Any]) -> String { message.map { "\($0.key): \($0.value)" }.joined(separator: ", ") }}
// Separate view for connection statusstruct ConnectionStatusView: View { @ObservedObject var connector: WatchConnector
var body: some View { HStack { Circle() .fill(connector.isReachable ? Color.green : Color.red) .frame(width: 12, height: 12)
Text(connector.isReachable ? "Connected" : "Disconnected") .font(.headline)
Spacer()
if connector.isActivated { Image(systemName: "checkmark.circle.fill") .foregroundColor(.green) } } }}
#Preview { ContentView()}步骤 6:配置 iOS 应用以支持 WatchConnectivity
步骤 6:配置 iOS 应用程序以支持 WatchConnectivity您的 iOS 应用程序还需要 WatchConnectivity 能力。
-
在项目导航器中,选择您的项目
-
选择您的 iOS 应用程序目标 (不是 watch 目标)
-
转到 签名和功能 选项卡
-
点击 + 功能
-
搜索并添加 WatchConnectivity (如果可用) 或者它可能会自动添加
-
Capgo插件Capacitor负责iOS端的处理,但确保Info.plist包含:
<key>WKCompanionAppBundleIdentifier</key><string>app.capgo.myapp.watchkitapp</string>
第 7 步:构建和运行
标题:第 7 步:构建和运行在模拟器上运行
标题:在模拟器上运行-
从 Xcode 窗口顶部的方案选择器中选择您的 watch 方案
-
选择一个 Apple Watch 模拟器(例如,“Apple Watch Series 9 (45mm)”)
- 点击设备选择器(位于方案选择器旁边)
- 选择一个 Apple Watch 模拟器(例如,“Apple Watch Series 9 (45mm)”)
-
点击 运行 或按
Cmd + R -
iOS模拟器将启动,显示iPhone和Apple Watch
在物理设备上运行
标题:在物理设备上运行-
连接您的iPhone通过USB
-
确保您的Apple Watch与该iPhone配对
-
选择您的watch方案
-
从设备列表中选择您的物理Apple Watch
-
点击 运行
-
第一次:您可能需要在两台设备上信任您的计算机
第 8 步:测试通信
第 8 步:测试通信从 iPhone (Capacitor) 到 Watch
从 iPhone (Capacitor) 到 Watch在您的 Capacitor 应用中:
import { CapgoWatch } from '@capgo/capacitor-watch';
// Check connectionconst info = await CapgoWatch.getInfo();console.log('Watch reachable:', info.isReachable);
// Send a messageif (info.isReachable) { await CapgoWatch.sendMessage({ data: { action: 'update', value: 'Hello from iPhone!' } });}从手表到iPhone
从手表到iPhone手表应用使用 WatchConnector:
// Send message (fire and forget)WatchConnector.shared.sendMessage(["action": "buttonTapped"])
// Send message with replyWatchConnector.shared.sendMessage(["request": "getData"]) { reply in print("Got reply: \(reply)")}在iPhone上处理消息
在iPhone上处理消息// Listen for messages from watchawait CapgoWatch.addListener('messageReceived', (event) => { console.log('Message from watch:', event.message); // { action: 'buttonTapped' }});
// Handle messages that need a replyawait CapgoWatch.addListener('messageReceivedWithReply', async (event) => { console.log('Request from watch:', event.message);
// Send reply back await CapgoWatch.replyToMessage({ callbackId: event.callbackId, data: { status: 'success', items: ['item1', 'item2'] } });});高级:自定义委托以获得更多控制
高级:自定义委托以获得更多控制如果您需要更多控制,请实现 WatchConnectorDelegate:
import SwiftUIimport CapgoWatchSDK
class WatchHandler: WatchConnectorDelegate { func didReceiveMessage(_ message: [String: Any]) { print("Received: \(message)") // Handle incoming message }
func didReceiveMessageWithReply(_ message: [String: Any], replyHandler: @escaping ([String: Any]) -> Void) { print("Received request: \(message)") // Process and send reply replyHandler(["status": "processed"]) }
func didReceiveApplicationContext(_ context: [String: Any]) { print("Context updated: \(context)") }
func didReceiveUserInfo(_ userInfo: [String: Any]) { print("User info received: \(userInfo)") }
func reachabilityDidChange(_ isReachable: Bool) { print("Reachability changed: \(isReachable)") }
func activationDidComplete(with state: WCSessionActivationState) { print("Activation completed: \(state.rawValue)") }}
// In your app setup:let handler = WatchHandler()WatchConnector.shared.delegate = handlerWatchConnector.shared.activate()手表应用未在手表上显示
Section titled “手表应用未在手表上显示”- 确保 bundle ID 正确关联(手表应用 bundle ID 应该是 iOS 应用 bundle ID +
.watchkitapp) - 检查两款应用都使用相同的团队签名
- 在物理设备上:在 iPhone 上打开手表应用 → 我的手表 → 滚动找到您的应用 → toggle ON
未接收到消息
Section titled “未接收到消息”- 验证两款应用都启用了 WCSession
- 检查
isReachable在发送消息之前 - 为了保证消息的可靠传递,请使用
transferUserInfo而不是sendMessage - 确保在其他设备发送消息之前注册监听器
”Session Not Activated” Error
Section titled “”Session Not Activated” Error”- Call
WatchConnector.shared.activate()在应用程序生命周期的早期调用 - 在 iOS 上,插件会自动激活 - 确保导入了插件
- 检查 iOS 目标中是否添加了 WatchConnectivity 能力
CapgoWatchSDK 构建错误
Section titled “Build Errors with CapgoWatchSDK”- 确保包添加到 watch target不支持iOS目标
- 清理构建文件夹: 产品 → 清理构建文件夹 (Cmd + Shift + K)
- 重置包缓存: 文件 → 包 → 重置包缓存
模拟器问题
模拟器问题- 重置模拟器: 设备 → 擦除所有内容和设置
- 确保 iOS 和 watchOS 模拟器是兼容的对
- 两台模拟器都需要运行,才能进行通信