mginn
mginn

Reputation: 16104

Watch connectivity session issues

I am trying to send a message from a watch extension to the phone in order to update a complication.

AppDelegate.swift

var session = WCSession.defaultSession()

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        if WCSession.isSupported(){
            session.delegate = self
            session.activateSession()
        }
        return true
}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {
        replyHandler(["scheduleNames":scheduleNames, "scheduleData":scheduleData])
    }

ExtensionDelegate.swift

override init(){
    super.init()
    if WCSession.isSupported(){
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
    self.sendMessage()
}

func sendMessage(){
        print("Attempting to send message")
        session.sendMessage(["Sender": "Complication"], replyHandler: {
            reply in
            print(reply.description)
            }, errorHandler: {
                error in
                print(error.description)
        })

    }

But when I run the watch simulator (and the phone app is not open), I receive

Error Domain=WCErrorDomain Code=7007 "WatchConnectivity session on paired device is not reachable."

This also comes on the physical phone and watch.

What could be causing this?

UPDATE
This only happens when I make the call from the extension. The watch app can send and receive the message fine. Also, it works when called from applicationDidFinishLaunching() but not anything else in the extension delegate.

UPDATE
I fixed the previous and now get

WatchConnectivity session has no delegate.

Upvotes: 1

Views: 2620

Answers (1)

mginn
mginn

Reputation: 16104

Thanks to this answer, I figured out the issue. Calling from the Complication (which is what I was doing) in the requestedUpdateDidBegin() executes an asynchronous method in an asynchronous method, resulting in the update function ending before the sendMessage function returns.

Upvotes: 2

Related Questions