Niall Kehoe
Niall Kehoe

Reputation: 379

Apple Watch Not Passing Data to iPhone - Swift

I'm trying to pass a String from my Apple Watch to an iPhone but it seems like it's not connecting. Here's my code:

ViewController.swift :

import UIKit
import WatchConnectivity

class ViewController: UIViewController, WCSessionDelegate {

    @IBOutlet weak var lablel: UILabel!
    var string = "Hello World"
    let session = WCSession.default()


    override func viewDidLoad() {
        super.viewDidLoad()

        session.delegate = self
        session.activate()

    }


    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
        let msg = message["StringValueSentFromiWatch"] as! String
        lablel.text = "Message : \(msg)"

        print("iphone recieved message")
    }

    func session(_ session: WCSession,
                 activationDidCompleteWith activationState: WCSessionActivationState,
                 error: Error?) {

    }

    func sessionDidBecomeInactive(_ session: WCSession) {

    }

    func sessionDidDeactivate(_ session: WCSession) {

    }
}

InterfaceController.swift :

import WatchKit
import Foundation
import WatchConnectivity


class InterfaceController: WKInterfaceController, WCSessionDelegate {

     let session = WCSession.default()

    override func willActivate() {
        super.willActivate()

        session.delegate = self
        session.activate()
    }

    @IBAction func SendPressed() {

        //Send Data to iOS
        let msg = ["StringValueSentFromiWatch" : "Hello World"]
        session.sendMessage(msg, replyHandler: { (replay) -> Void in
            print("apple watch sent")
        }) { (error) -> Void in
         print("apple watch sent error")
        }

    }

    func session(_ session: WCSession,
                 activationDidCompleteWith activationState: WCSessionActivationState,
                 error: Error?){
    }

}

I'm trying to send "Hello World" to the iPhone but I get this printout in the console:

errorHandler: YES with WCErrorCodePayloadUnsupportedTypes

and 'apple watch sent error'.

I know it's not sending but I don't know why. Does anyone know why this doesn't work?

Note: I'm running this is the simulator but I'm fairly sure this is not the problem.

Upvotes: 2

Views: 2397

Answers (1)

Jens Peter
Jens Peter

Reputation: 765

I think you have messed up in the sendMessage(), I cannot work out the replyHandler syntax, and you miss the errorHandler: parameter.

Anyway, I've tried your code, and with a few changes it would work.

1). In InterfaceController, the sendPressed():

    var count = 0    
@IBAction func SendPressed() {
    //Send Data to iOS
    let msg = ["Count" : "\(count)"]

    if session.isReachable {
        session.sendMessage(msg, replyHandler: nil, errorHandler: { (error) -> Void in
            print("Error handler: \(error)")
        })
        count += 1
    }
}

I've added a count, since the message must vary for each call (to conserve battery), so you can now press the button several times in a row. And a check to verify that the host application is reachable.

2.) In the ViewController, remember to update the GUI on the main thread:

    func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
    DispatchQueue.main.async {
        self.lablel.text = "Message : \(message)"
    }
}

Otherwise the label will not update when you receive the data.

Let me know if it helps you!

Upvotes: 3

Related Questions