WatchOS 配套应用程序构建成功,但在存档时出现 WCSessionDelegate 错误

问题描述 投票:0回答:1

在 WatchOS 配套应用程序中,我有这个模型:

import Foundation
import WatchConnectivity

class WatchViewModel: NSObject, ObservableObject {
    var session: WCSession
    @Published var counter = 0
    
    // Add more cases if you have more receive method
    enum WatchReceiveMethod: String {
        case sendCounterToNative
    }
    
    // Add more cases if you have more sending method
    enum WatchSendMethod: String {
        case sendCounterToFlutter
    }
    
    init(session: WCSession = .default) {
        self.session = session
        super.init()
        self.session.delegate = self
        session.activate()
    }
    
    func sendDataMessage(for method: WatchSendMethod, data: [String: Any] = [:]) {
        sendMessage(for: method.rawValue, data: data)
    }
    
}

extension WatchViewModel: WCSessionDelegate {
    

        public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?)
        {
        }


    
    // Receive message From AppDelegate.swift that send from iOS devices
    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
        DispatchQueue.main.async {
            guard let method = message["method"] as? String, let enumMethod = WatchReceiveMethod(rawValue: method) else {
                return
            }
            
            switch enumMethod {
            case .sendCounterToNative:
                self.counter = (message["data"] as? Int) ?? 0
            }
        }
    }
    
    func sendMessage(for method: String, data: [String: Any] = [:]) {
        guard session.isReachable else {
            return
        }
        let messageData: [String: Any] = ["method": method, "data": data]
        session.sendMessage(messageData, replyHandler: nil, errorHandler: nil)
    }
    
}

我可以成功构建应用程序并在模拟器上运行它。但是当我尝试存档应用程序时,我收到错误:

类型“WatchViewModel”不符合协议“WCSessionDelegate”。

错误消息听起来像是我的 WCSessionDelegate 缺少必需的方法,但我似乎找不到它。

但是为什么在模拟器上运行成功呢?另外,如果我将 Archive 方案更改为 Debug 而不是 Release,那么 Archive 就会成功。

我被困住了。我不知道如何追踪为什么这个错误会发生在调试方案中而不是发布方案中。有人能指出我正确的方向吗?

ios swift xcode watchkit watchos
1个回答
0
投票

我遇到了完全相同的问题。你找到解决办法了吗?

© www.soinside.com 2019 - 2024. All rights reserved.