-1

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()
            }
        }
    }
}




  • [Here](https://stackoverflow.com/questions/68071301/saving-userinfo-received-via-watchconnectivity-to-coredata/68085918#68085918) is a working sample. It uses core data so you may need a new sample project if not just comment out that section – lorem ipsum Nov 28 '22 at 21:27

1 Answers1

0

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
    }
}
  • I already tried to debug the code but the session doesn't get activated in the first place, the activation state is "Not Activated" and I get "[WC] WCSession has not been activated" plus device not reachable – Ahmed Kazez Nov 28 '22 at 19:38
  • what method do you use to send message? – Oleksandr Bielov Nov 28 '22 at 20:44