使用WatchConnectivity将名称和生日发送到Watch

时间:2017-01-18 15:01:41

标签: ios swift xcode watchconnectivity

我正在访问用户联系人,以便将联系人名称和生日从iPhone发送到AppleWatch。我可以访问联系人并可以在手机上显示数据,但我在使用WatchConnectivity发送信息时遇到问题。我正在使用Ray Wenderlich书WatchOS by Tutorials作为指导,但我仍然遇到麻烦。

以下是我在电话方面设置AppDelegate.swift中的观看连接功能的方法。

// MARK:  Watch Connectivity
extension AppDelegate: WCSessionDelegate {

func sessionDidBecomeInactive(_ session: WCSession) {
    print("WC Session did become inactive.")
}

func sessionDidDeactivate(_ session: WCSession) {
    print("WC Session did deactivate.")
    WCSession.default().activate()
}


func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if let error = error {
        print("WC Session activation failed with error:" + "\(error.localizedDescription)")
        return
    }
    print("Phone activated with state:" + "\(activationState.rawValue)")
}

func setUpWatchConnectivity() {
    if WCSession.isSupported() {
        let session = WCSession.default()
        session.delegate = self
        session.activate()
        }
    }

}

这在ViewController.swift

// MARK Watch Connectivity

extension ViewController {


func sendNameAndBirthdayToWatch() {
    if WCSession.isSupported() {
        let session = WCSession.default()
        if session.isWatchAppInstalled {
            let nameAndBirthday = ["name": nameLabel.text, "birthday": birthdayLabel.text]
            do {
                try session.updateApplicationContext(nameAndBirthday)
            } catch {
                print("Error")
            }
         }
     }
  }

}

我在手机上用UIButton打电话给sendNameAndBirthdayToWatch()。作为原型,手机当前在Xcode模拟器中从Contacts中提取的两个单独标签上显示名称和生日,所以我至少知道我正在获取数据。

在我ExtensionDelegate.swift的手表上。

// MARK: Watch Connectivity
extension ExtensionDelegate: WCSessionDelegate {

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if let error = error {
        print("Activation failed with error: \(error.localizedDescription)")
        return
    }
    print("Watch activated with activation state: \(activationState.rawValue) ")
}

func setupWatchConnectivity() {
    if WCSession.isSupported() {
        let session = WCSession.default()
        session.delegate = self
        session.activate()
    }
}

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
    if let name = applicationContext["name"] as? String, let birthday = applicationContext["birthday"] as? String {
        InterfaceController.sharedInterfaceController.updateWatchLabel(name: name, birthday: birthday)
    }
}
}

这就是我被困住的地方。我不确定我哪里出错和/或如何前进。

我正在观看updateWatchLabel()中的InterfaceController.swift,但我没有看到任何结果。

提前感谢您的帮助。我一直在盯着这个坚实的一周,不幸的是,Wenderlich书中的示例项目对我的理解来说有点过于复杂。

我正在使用Xcode 8.2.1和Swift 3。

1 个答案:

答案 0 :(得分:1)

我及时回答了自己的问题。

以下是ViewController.swift手机上的代码。代码与AppDelegate.swift中的原始帖子保持一致。

    func sendMessageToWatch() {
    if WCSession.isSupported() {
        let session = WCSession.default()
        if session.isWatchAppInstalled {
            do {
                let message = ["message": "A message as String"]
                try session.updateApplicationContext(message)
            } catch {
                print("Error: \(error)")
            }

        } else if session.isWatchAppInstalled == false {
            print("No watch app installed")
        }
    }
}

我正在通过按手机上的UIButton来调用此功能。然后在InterfaceController.swift的观察端接收端。我基本上将代码从ExtensionDelegate.swift移到了InterfaceController.swift

    // MARK: Watch Connectivity
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if let error = error {
        print("Activation failed with error: \(error.localizedDescription)")
        return
    }
    print("Watch activated with activation state: \(activationState.rawValue) ")
}


func setupWatchConnectivity() {
    if WCSession.isSupported() {
        let session  = WCSession.default()
        session.delegate = self
        session.activate()
    }
}

func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
    let message = applicationContext["message"] as? String

    DispatchQueue.main.async(execute:  {
        self.label.setText(message)
    })

}