0

It's my first time trying WatchConnectivity kit. When I attempt to send ApplicationContext from phone, my debug print indicates that updateApplicationContext is working correctly (with isPaired/reachable/isActivated/isWatchAppInstalled checked before sending the update). However, I'm having trouble receiving it form the paired watch simulator as nothing is shown on that end. Here are the code for the Phone Extension:

    func sendDataToWatch(data: String) {

        state.append(data)

        do {

            NSLog("sent by phone")

            try WCSession.default.updateApplicationContext(["currentstate": state])

        }

        catch {

            print(error)

        }

    }

Here are the code for watch:

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String: Any]) {

        print("Hello")

        if let getState = applicationContext["currentstate"] as? [String]{

            print("\(getState)")

            self.state = getState[0]

        }

    }

Any suggestion would be appreciated!

  • Have you tried this from official apple documentation. This method can only be called while the session is active—that is, the activationState property is set to WCSessionActivationState.activated. Calling this method for an inactive or deactivated session is a programmer error. – Chanpreet Singh Jun 16 '21 at 06:15
  • @ChanpreetSingh I do have a method called isActivated() to check that property, it does return true. – Gefei Shen Jun 16 '21 at 06:37
  • @ChanpreetSingh here is the code: ``` func isActivated() -> Bool{ return self.session.activationState == WCSessionActivationState.activated } ``` – Gefei Shen Jun 16 '21 at 06:38
  • try some solution from this might help https://stackoverflow.com/questions/32802894/watchkit-didreceiveapplicationcontext-not-being-called – Chanpreet Singh Jun 16 '21 at 07:59
  • @chanpreetSingh Thanks for that, but it did not resolve my problem. – Gefei Shen Jun 16 '21 at 09:08

0 Answers0