In the WatchOS companion app I have this model:
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)
}
}
I can Build the app successfully and run it on the simulator. But when I try to Archive the app I receive the error:
Type 'WatchViewModel' does not conform to protocol 'WCSessionDelegate'.
The error message makes it sound like my WCSessionDelegate is missing a required method, but I can't seem to find it.
But why does it run on successfully on the simulator then? Also, if I change the Archive scheme to Debug instead of release, then the Archive is successful.
I'm stuck. I do not know how to track down why this error would happen in Debug scheme but not Release. Can anyone point me in the right direction?
It's trying to build a release build of the watch app for iOS, and on iOS that WCSessionDelegate requires two extra methods.
Go through all of your build settings and make sure you set the watch app to ONLY build for WatchOS, and the main app to only build for iOS