Watch Simulator 未加载

Watch Simulator Not Loading

提问人:Deven Nahata 提问时间:7/12/2023 最后编辑:burnsiDeven Nahata 更新时间:7/24/2023 访问量:47

问:

目前正在尝试创建一个应用程序,该应用程序使用 WCSession 将数据/消息从 Apple Watch 发送到链接的 iPhone。代码编译时没有错误,但仅打开 iOS 模拟器并显示 WCSession 未配对。因此,我无法测试该程序。

我尝试打开 watchOS 模拟器并尝试另一个模拟器,但它没有用。这是代码,以防出现问题。

手表应用程序

import SwiftUI
import WatchConnectivity

class ContentViewModel: NSObject, ObservableObject, WCSessionDelegate {
    @Published var message: String = ""
    
    override init() {
        super.init()
        setupWatchConnectivity()
    }
    
    func setupWatchConnectivity() {
        if WCSession.isSupported() {
            let session = WCSession.default
            session.delegate = self
            session.activate()
        }
    }
    
    func sendMessageToPhone() {
        if WCSession.default.isReachable {
            let message = ["text": "Hello, iPhone!"]
            WCSession.default.sendMessage(message, replyHandler: nil, errorHandler: nil)
        }
    }
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        // Handle activation completion
    }
    
    func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
        if let text = message["text"] as? String {
            DispatchQueue.main.async {
                self.message = text
            }
        }
    }
}

struct ContentView: View {
    @StateObject private var viewModel = ContentViewModel()
    
    var body: some View {
        VStack {
            Text("Message from iPhone:")
            Text(viewModel.message)
                .font(.title)
            
            Button(action: viewModel.sendMessageToPhone) {
                Text("Send Message to iPhone")
            }
        }
        .onAppear {
            viewModel.setupWatchConnectivity()
        }
    }
}

iOS应用程序

import SwiftUI
import WatchConnectivity

class ContentViewModel: NSObject, ObservableObject, WCSessionDelegate {
    @Published var message: String = ""
    
    override init() {
        super.init()
        setupWatchConnectivity()
    }
    
    func setupWatchConnectivity() {
        if WCSession.isSupported() {
            let session = WCSession.default
            session.delegate = self
            session.activate()
        }
    }
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        // Handle activation completion
    }
    
    func session(_ session: WCSession, didReceiveMessage message: [String: Any]) {
        if let text = message["text"] as? String {
            DispatchQueue.main.async {
                self.message = text
            }
        }
    }
}

struct ContentView: View {
    @StateObject private var viewModel = ContentViewModel()
    
    var body: some View {
        VStack {
            Text("Message from Apple Watch:")
            Text(viewModel.message)
                .font(.title)
        }
        .onAppear {
            viewModel.setupWatchConnectivity()
        }
    }
}
Swift Xcode IOS 模拟器 WatchKit WCSession

评论

0赞 lorem ipsum 7/12/2023
您必须确保您选择的 iPhone 已配对手表。您可以添加新的模拟器以确保
0赞 Deven Nahata 7/13/2023
我试过这个,但没有用。点按顶部的模拟器图标,创建新的 SIM 卡,并配对 Apple Watch。

答: 暂无答案