WatchConnectivity

Good morning,

I come to you for a question: When I install my application on my iPhone for the first time, and I install the watch application from the native "Watch" application, the Watch Connectivity function does not work, I have to do the installation from Xcode to the watch for this function to work. Is this normal? if yes, the problem will not arise during a publication? I have the same problem when using watch and iPhone simulators, WatchConnectivity does not work.

I am this error code in Xcode:

-[WCSession handleIncomingUserInfoWithPairingID:]_block_invoke delegate (null) does not implement session:didReceiveUserInfo:, discarding incoming content

Here is the code for the iPhone and the watch:

In my iPhone app:

import WatchConnectivity

let userDefaultsDataVenantWatch = UserDefaults.standard

class PhoneDataModel : NSObject, WCSessionDelegate, ObservableObject {
    static let shared = PhoneDataModel()
    let session = WCSession.default
    @Published var TableauSynchroIphoneVersWatch : [String:String] = ["0":"0"]
    @Published var dataWatchVersIphone: [String:String] = ["":""]
    
    override init() {
        super.init()
        if WCSession.isSupported() {
            session.delegate = self
            session.activate()
        } else {
            print("ERROR: Watch session not supported")
        }
    }
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if let error = error {
            print("session activation failed with error: \(error.localizedDescription)")
            return
        }
    }
    func sessionDidBecomeInactive(_ session: WCSession) {
        session.activate()
    }
    func sessionDidDeactivate(_ session: WCSession) {
        session.activate()
    }
    

    
    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any]) {
        guard let newCount = userInfo["TableauSynchroIphoneWatch"] as? [String:String] else {
            print("ERROR: unknown data received from Watch TableauSynchroIphoneWatch")
            return
        }
        
        DispatchQueue.main.async {

            print(newCount)

        }
    }
}

In my Watch app:

import WatchConnectivity

let userDefaultsDataVenantIphone = UserDefaults.standard
var TableauVenantIphone:[String:String] = ["":""]

class WatchDataModel : NSObject, WCSessionDelegate, ObservableObject {
    static let shared = WatchDataModel()
    let session = WCSession.default
    @Published var TableauSynchroIphoneWatch : [String:String] = ["0":"0"]
    

    override init() {
        super.init()
        if WCSession.isSupported() {
            session.delegate = self
            session.activate()
        } else {
            print("ERROR: Watch session not supported")
        }
    }
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if let error = error {
            print("session activation failed with error: \(error.localizedDescription)")
            return
        }
    }
    
    
     
    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any]) {
        guard let newCount = userInfo["TableauSynchroIphoneVersWatch"] as? [String:String] else {
            print("ERROR: unknown data received from Watch TableauSynchroIphoneWatch")
            return
        }
     
        
        DispatchQueue.main.async {
            print(newCount)
           
        }
    }
}

Thank for your answers !

Where's the code where you send the userInfo?

Secondly, these methods aren't simply for you to activate the session; you need to handle the state change, not attempt to reactivate the session:

    func sessionDidBecomeInactive(_ session: WCSession) {
        session.activate()
    }
    func sessionDidDeactivate(_ session: WCSession) {
        session.activate()
    }

If the session isn't active, and you try to send a message it's a programmer error.

Hello,

Thank you for your help.

On my main program, I ask to send information this way:

TableauAEnvoyer["TypeDeSeancePourLaSeance\(Load_nbSeancePagingView)"] = "\(SeanceSelectionnee)"
WatchDataModel.shared.TableauSynchroIphoneWatch = TableauAEnvoyer
WatchDataModel.shared.session.transferUserInfo(["TableauSynchroIphoneWatch":WatchDataModel.shared.TableauSynchroIphoneWatch])

I'm modify my code in my Watch app, but Xcode gives me an error:

Cannot override 'sessionDidBecomeInactive' which has been marked unavailable

Cannot override 'sessionDidDeactivate' which has been marked unavailable

In my Watch app, I'm add your code :

import WatchConnectivity

let userDefaultsDataVenantIphone = UserDefaults.standard
var TableauVenantIphone:[String:String] = ["":""]

class WatchDataModel : NSObject, WCSessionDelegate, ObservableObject {
    static let shared = WatchDataModel()
    let session = WCSession.default
    @Published var TableauSynchroIphoneWatch : [String:String] = ["0":"0"]
    

    override init() {
        super.init()
        if WCSession.isSupported() {
            session.delegate = self
            session.activate()
        } else {
            print("ERROR: Watch session not supported")
        }
    }
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        if let error = error {
            print("session activation failed with error: \(error.localizedDescription)")
            return
        }
    }
    
    func sessionDidBecomeInactive(_ session: WCSession) {
        session.activate()
    }
    func sessionDidDeactivate(_ session: WCSession) {
        session.activate()
    }
     
    func session(_ session: WCSession, didReceiveUserInfo userInfo: [String : Any]) {
        guard let newCount = userInfo["TableauSynchroIphoneVersWatch"] as? [String:String] else {
            print("ERROR: unknown data received from Watch TableauSynchroIphoneWatch")
            return
        }
     

        
        DispatchQueue.main.async {
            print(newCount)
           
        }
    }
}
WatchConnectivity
 
 
Q