0

Hi I have this function

extension UIViewController: WCSessionDelegate {
public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {        
}
public func sessionDidBecomeInactive(_ session: WCSession) {

}
public func sessionDidDeactivate(_ session: WCSession) {
}


//MARK: -SYNC DATA TO APPLE WATCH

func syncToAppleWatch(){
    var session: WCSession?
    if WCSession.isSupported() {//4.1
      session?.delegate = self
      session = WCSession.default//4.2
      session?.activate()//4.4
    }

}

I call "syncToAppleWatch" inside DidLoad. But I receive this error

 [WC] denying activation due to missing delegate
 [WC] WCSession has not been activated

How Can I fix it?

TheCesco1988
  • 280
  • 1
  • 2
  • 10

1 Answers1

3

These two lines make no sense

session?.delegate = self  // session is still nil here, delegate won't be set
session = WCSession.default

change the order to

session = WCSession.default
session?.delegate = self 
Sulthan
  • 128,090
  • 22
  • 218
  • 270