Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data to Apple Watch app

I am trying to pass data from my app into my Apple Watch app. Basically, I am using the same method as I used for creating the today widget and so I am passing data through NSUserDefaults.

The problem is, that when I run my app, the data does not update the labels in the Watch app as I would expect it to.

Here is what I have...

override init(context: AnyObject?) {     // Initialize variables here.     super.init(context: context)      // Configure interface objects here.     NSLog("%@ init", self)      var defaults = NSUserDefaults(suiteName: "group.AffordIt")     var totalBudgetCalculation = ""     if (defaults!.stringForKey("totalBudgetWidget") != nil) {             println("Worked")         totalBudgetCalculation = defaults!.stringForKey("totalBudgetWidget")!         initialBudgetLabel.setText("Initial: \(totalBudgetCalculation)")     }      var currentBudgetCalculation = ""     if (defaults!.stringForKey("currentBudgetWidget") != nil) {         currentBudgetCalculation = defaults!.stringForKey("currentBudgetWidget")!         currentBudgetLabel.setText("Current: \(currentBudgetCalculation)")     } } 

I tried putting this code in willActivate(), however that doesn't seem to make a difference.

Anyone know where I am going wrong?

like image 945
user3746428 Avatar asked Nov 19 '14 19:11

user3746428


People also ask

How do I transfer data to my Apple Watch?

To add data, go to: Health Data > Activity > tap on Active Energy > tap on the "+" sign (upper-right) > enter your data. Neither Stand Hours nor Exercise Minutes can be added manually.

Can I use data from iPhone to Apple Watch?

Open Control Center on your iPhone, then make sure that Wi-Fi and Bluetooth are on. Your Apple Watch uses Wi-Fi and Bluetooth to communicate with your paired iPhone. If you have cellular, your watch can also stay connected through a cellular network.

Can you share cellular data with Apple Watch?

When your iPhone is off or out of range, your Apple Watch can use a Wi-Fi network to send and receive data. Your watch can also connect to a cellular network if it's a cellular model. And if you've set up an Apple Watch for a family member, they can use a cellular or Wi-Fi connection with their watch.


1 Answers

This applies to OS 1 only. See below for better answers.

I got it working using your method. I guess there's a couple of things you can check:

1) Are you synchronising the defaults after you set the value:

defaults?.synchronize(); NSLog("%@ ", defaults?.dictionaryRepresentation()) 

2) Have you enabled the App Group in both your app and your extension?

App Group capability for App TargetApp Group capability for Watch Extension Target

3) Are you using the correctly named app group when constructing the NSDefaults? For example, I use:

NSUserDefaults(suiteName: "group.com.brindysoft.MyWatch"); 

Once all that's set up I run the app, set the value in the defaults, then run the glance target which reads the value from the default and that seems to work!

enter image description here

  1. Still stuck? check your app groups in your apple account
like image 166
brindy Avatar answered Sep 21 '22 16:09

brindy