You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用Swift实现从iPhone启动配对Apple Watch上的自定义应用

从iPhone启动配对的Apple Watch应用实现方案

嘿,这个需求我刚好有实践经验,用Apple的WatchConnectivity框架就能轻松实现,前提是你的iPhone和Watch已经配对,且Watch端应用已经安装。下面是具体的Swift代码实现方案,我会拆成步骤讲清楚:

第一步:项目配置检查

  • 确保你的iPhone和Watch Extension/Watch App target都正确配置了Watch Connectivity能力(在Xcode的Capabilities面板里打开即可,Xcode通常会自动处理相关配置)。
  • 确认两个target的Bundle ID是关联的(通常Watch App的Bundle ID是iPhone App的Bundle ID加上.watchkitapp后缀,Extension是.watchkitextension)。

第二步:iPhone端代码实现

首先导入WatchConnectivity框架,然后激活WCSession,最后调用启动Watch App的方法。以下是完整的代码示例,你可以直接移植到你的项目中:

import UIKit
import WatchConnectivity

class MainViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // 初始化并激活WCSession,这是使用WatchConnectivity的前提
        setupWCSession()
    }

    // 触发启动Watch App的按钮点击事件
    @IBAction func launchWatchAppButtonTapped(_ sender: UIButton) {
        let session = WCSession.default
        
        // 先检查必要条件:已配对 + Watch端应用已安装
        guard session.isPaired, session.isWatchAppInstalled else {
            showAlert(title: "提示", message: "Apple Watch未配对或Watch端应用未安装")
            return
        }
        
        // 尝试启动Watch端应用
        session.openWatchApp { [weak self] success in
            DispatchQueue.main.async {
                if success {
                    print("Watch应用启动成功")
                    self?.showAlert(title: "成功", message: "Watch应用已启动")
                } else {
                    print("Watch应用启动失败")
                    self?.showAlert(title: "失败", message: "Watch应用启动失败,请重试")
                }
            }
        }
    }
    
    // 初始化WCSession
    private func setupWCSession() {
        guard WCSession.isSupported() else {
            print("当前设备不支持WatchConnectivity")
            return
        }
        
        let session = WCSession.default
        session.delegate = self
        session.activate()
    }
    
    // 封装的弹窗提示方法
    private func showAlert(title: String, message: String) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "确定", style: .default))
        present(alert, animated: true)
    }
}

// 实现WCSessionDelegate的必要方法
extension MainViewController: WCSessionDelegate {
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if let error = error {
            print("WCSession激活失败: \(error.localizedDescription)")
        } else {
            print("WCSession激活成功")
        }
    }
    
    // iOS 13及以上版本必须实现的空方法
    func sessionDidBecomeInactive(_ session: WCSession) {}
    func sessionDidDeactivate(_ session: WCSession) {}
}

额外注意事项

  • 如果需要在启动Watch App后直接跳转到特定界面,可以在启动成功后,通过WCSession的sendMessage(_:replyHandler:errorHandler:)方法给Watch端发送跳转指令,Watch端监听消息后执行界面跳转逻辑。
  • 测试时,建议使用真实的Apple Watch设备(模拟器也可以,但需要确保配对的Watch模拟器处于运行状态)。
  • 启动失败的常见原因:Watch未配对、Watch端应用未安装、WCSession未正确激活、设备蓝牙/连接异常等,可以通过控制台日志和弹窗提示排查问题。

内容的提问来源于stack exchange,提问作者Venkatesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:55:09