将数据传递到Apple Watch应用程序 [英] Passing data to Apple Watch app

查看:132
本文介绍了将数据传递到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.

这就是我所拥有的......

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

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

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

任何人都知道我哪里出错了吗?

Anyone know where I am going wrong?

推荐答案

我使用你的方法工作了。我想你可以检查几件事:

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

一旦完成设置我运行应用程序,将值设置为默认值,然后运行一瞥从默认值中读取值的目标,看起来有效!

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应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆