WatchOS WCSession won't activate / WCsession missing its delegate

499 Views Asked by At

Im trying to simply connect an iOS app to a watchOS app with Xcode emulators and using watch connectivity WCSession. The watch isn't reachable and session won't activate even though I tried every possible change to the code. I use the same Sync service that I change its target to both watch and iOS apps and I call them as soon as app starts using .didAppear Am I doing something wrong ?





import Foundation
import WatchConnectivity

class SyncService : NSObject, WCSessionDelegate {
    
    
    private var session: WCSession = .default

    init(session: WCSession = .default) {
        
        
        super.init()
        self.session = session
        self.session.delegate = self
        print("Reachable :",session.isReachable)
        #if os(iOS)
        print("Connection provider initialized on phone")
        #endif
        #if os(watchOS)
        print("Connection provider initialized on watch")
        #endif
        
        self.connect()
        print("Details",self.session)
    }
    
    func connect() {
        guard WCSession.isSupported() else {
            print("WCSession is not supported")
            return
        }
        switch self.session.activationState {
                case .activated:
                    print("WCSession activated successfully")
                case .inactive:
                    print("Unable to activate the WCSession. Error:")
                case .notActivated:
                    print("Unexpected .notActivated state received after trying to activate the WCSession")
                @unknown default:
                    print("Unexpected state received after trying to activate the WCSession")
                }
        session.activate()
        
    }
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        
    }

    #if os(iOS)
    func sessionDidBecomeInactive(_ session: WCSession) { }
    
    func sessionDidDeactivate(_ session: WCSession) { }
    #endif
    
        
     //   func sendMessage(_ key: String, _ message: String, _ errorHandler: ((Error) -> Void)?) {
        //    if session.isReachable {
        //        session.sendMessage([key : message], replyHandler: nil) { (error) in
         //           print(error.localizedDescription)
         //           if let errorHandler = errorHandler {
           ///             errorHandler(error)
            //        }
        //        }
       //     }
   //     }
        
    

        
        
    }







import SwiftUI

@main
struct Connectivity_Watch_AppApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView() .onAppear{
                var model = SyncService()
            }
        }
    }
}





import SwiftUI

@main
struct ConnectivityApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView() .onAppear{
                var model = SyncService()
            }
        }
    }
}




1

There are 1 best solutions below

2
Oleksandr Bielov On

You have to wait until session will be activated and only then you can send message. So, check there is session is activated and then send message

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if activationState == .activated {
        // send message
    }
}