本文介绍了将数据传递给 Apple Watch 应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据从我的应用程序传递到我的 Apple Watch 应用程序.基本上,我使用的方法与创建今天的小部件时使用的方法相同,因此我通过 NSUserDefaults 传递数据.

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.

问题是,当我运行我的应用程序时,数据并没有像我期望的那样更新 Watch 应用程序中的标签.

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.

这是我所拥有的...

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)")
    }
}

我尝试将这段代码放在 willActivate() 中,但这似乎没什么区别.

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

有人知道我哪里出错了吗?

Anyone know where I am going wrong?

推荐答案

这仅适用于 OS 1.请参阅下文以获得更好的答案.

我用你的方法让它工作了.我想您可以检查以下几点:

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

1) 您是否在设置值后同步默认值:

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

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

2) 您是否在应用和扩展程序中都启用了应用组?

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

3) 您在构建 NSDefaults 时是否使用了正确命名的应用程序组?例如,我使用:

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

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

所有设置完成后,我运行应用程序,将值设置为默认值,然后运行从默认值中读取值的 Glance 目标,这似乎有效!

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!

  1. 还卡住了吗?检查您的苹果帐户中的应用程序组

这篇关于将数据传递给 Apple Watch 应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-01 18:47