27704

Apple Watch Not Passing Data to iPhone - Swift

Question:

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:

<blockquote>

errorHandler: YES with WCErrorCodePayloadUnsupportedTypes

</blockquote>

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.

Answer1:

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!

Recommend

  • Issue #1 when compiling Xuggler for Raspberry pi with libspeex
  • NSTimer causes “unrecognized selector” crash when it fires
  • How to check platform at runtime
  • Use ng-model in directive and controller
  • Setting Access-Control-Allow-Origin (CORS) in the Rackspace Cloud Files Python API
  • Converting Excel column letters to corresponding column numbers
  • Java Play2- Akka for jobs
  • How to setup a Web API Facade pattern
  • Attempting to combine Angular UI Bootstrap's Typeahead with Alert
  • How to call a html page and make it as pop up window in JavaScript?
  • How to find MAX or MIN in Verilog coding?
  • Ternary Operators & Maintenance [closed]
  • Using Sybase ASE BCP to a Remote Server
  • IKEA Style Lightbox
  • Having several issues with a Python service for Windows
  • Upload document to specific folder in a SharePoint Document library using WebClient
  • On Woocommerce order complete, activate woosensei course
  • Downgrade NG2 Directive to AngularJS
  • Silverlight MVVM, stop SelectionChanged triggering in response to ItemsSource reset
  • Check if a file is a specific type in C
  • Spring annotation @Order
  • Extract table from Powerpoint
  • Adding Dynamic Row and Data on Checkbox Click
  • Find corners of a rotated rectangle
  • Example of using Service Exists MSBuild task in Microsoft.Sdc.Tasks?
  • Circular Left Rotation Algorithm in C#
  • Use AutoIt with java applications
  • Arraylist of strings into one comma separated string
  • What does “T extends Junk” mean in a generic class in Java?
  • How do I use libcurl to printf a remote FTP directory listing?
  • xpath assertion failure with dynamic xpath
  • how to run a different select statement based on condition in Hive SQL
  • How to clear a browser cache in Protractor
  • read part of h5 dataset python
  • Firebase: How to read from external DB?
  • convert json to excel in java
  • ReferenceError: TextEncoder is not defined