WatchKit didReceiveApplicationContext 未被调用 [英] WatchKit didReceiveApplicationContext not being called

查看:28
本文介绍了WatchKit didReceiveApplicationContext 未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

无法调用didReceiveApplicationContext.有什么想法吗?

I can't get didReceiveApplicationContext to be called. Any ideas?

InterfaceController:

import WatchKit
import Foundation
import WatchConnectivity

class InterfaceController: WKInterfaceController, WCSessionDelegate {

    @IBOutlet var colorLabel: WKInterfaceLabel!

    private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil

    override init() {
        super.init()
        session?.delegate = self
        session?.activateSession()
    }

    override func awakeWithContext(context: AnyObject?) {
        super.awakeWithContext(context)
    }

    func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]){
        let colors : String = applicationContext["color"] as! String
        colorLabel.setText(colors)
        NSLog("session did receive application context")
    }

}

我一直在关注本教程:http://www.kristinathai.com/watchos-2-how-to-communicate-between-devices-using-watch-connectivity/

不会发生 NSLogcolorLabel 的设置.不知道我错过了什么.谢谢!

No NSLog or setting of the colorLabel happens. No idea what I'm missing. Thanks!

推荐答案

我将上面的代码复制到一个新的手表应用中,它运行良好.错误必须出在发送方.您确定正在调用 iOS 应用程序中的代码吗?我想您正在使用 Xcode 和两个模拟器,一个用于 iOS 应用,另一个用于 WatchApp.

I copied the above code into a new watch app and it works fine. The error must lay on the sending side. Are you certain the code in the iOS app is being called? I presume you are using Xcode and two simulators, one for the iOS app and one for the WatchApp.

除非在手机模拟器上打开应用程序,否则不会运行iOS端的代码.您在 iOS 端的何处以及如何发出 updateAppContext 调用?

The code on the iOS side is not run unless you open the app on the phone simulator. Where and how on the iOS side are you issuing the updateAppContext call?

在我的测试中,这是我在 iOS 端添加到我的 ViewController.swift 的所有内容(直到我在 iPhone 上启动 iOS 应用程序才会触发此代码.)

In my test, this is all that I added to my ViewController.swift on the iOS side (This code will not be triggered until I start the iOS app on my iPhone.)

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    let session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()
    do {
        try session.updateApplicationContext( ["color" : "Red" ])
    } catch _ {

    }
}

这篇关于WatchKit didReceiveApplicationContext 未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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