WCSession.sendMessage工作50/50

最近,我正在研究一个与“手表/ iPhone通信”有关的项目。 但是我的代码有时是有效的,有时候不起作用,这对我来说很奇怪,因为我认为代码应该工作或者不工作。 它不能是50/50。 所以,我不知道出了什么问题。

在iPhone上设置WCSession:

class WatchCommunicationController: NSObject, WCSessionDelegate { var session : WCSession? override init(){ // super class init super.init() // if WCSession is supported if WCSession.isSupported() { // it is supported // get default session session = WCSession.defaultSession() // set delegate session!.delegate = self // activate session session!.activateSession() } else { print("iPhone does not support WCSession") } } ... ... } 

在Watch上设置类似的WCSession:

 class PhoneCommunicationController: NSObject, WCSessionDelegate { var session : WCSession? override init(){ // super class init super.init() // if WCSession is supported if WCSession.isSupported() { // it is supported // get default session session = WCSession.defaultSession() // set delegate session!.delegate = self // activate session session!.activateSession() } else { print("Watch does not support WCSession") } } ... ... } 

发送信息观察:

func sendGesture(手势:GKGesture){

 // if WCSession is reachable if session!.reachable { // it is reachable // create the interactive message with gesture let message : [String : AnyObject] message = [ "Type":"Gesture", "Content":gesture.rawValue ] // send message session!.sendMessage(message, replyHandler: nil, errorHandler: nil) print("Watch send gesture \(gesture)") } else{ // it is not reachable print("WCSession is not reachable") } 

}

相关的枚举:

 enum GKGesture: Int { case Push = 0, Left, Right, Up, Down } 

在iPhone上接收消息:

 func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) { //retrieve info let type = message["Type"] as! String let content = message["Content"] switch type { case "Gesture": handleGesture(GKGesture(rawValue: content as! Int)!) default: print("Received message \(message) is invalid with type of \(type)") } } func handleGesture(gesture : GKGesture){ print("iPhone receives gesture \(gesture)") var notificationName = "" switch gesture { case .Up: notificationName = "GestureUp" case .Down: notificationName = "GestureDown" case .Left: notificationName = "GestureLeft" case .Right: notificationName = "GestureRight" case .Push: notificationName = "GesturePush" } NSNotificationCenter.defaultCenter().postNotificationName(notificationName, object: nil) } 

不知何故,我无法在Xcode上debugging我的Watch应用程序,debugging会话不会附加。 我不知道为什么。 所以,我只用iPhonedebugging片面。

有时候我会打印出“接收手势”,有时候不会,而且通知也一样。

我不知道在WCSession内传输的时候Int是否会被NSNumber包装。 如果是这样,那么这就是为什么当我使用Int作为枚举的基类时,它将无法正常工作,当String是基类。

连接性已知问题在使用WCSession API使用NSNumber和NSDate对象时,您的应用程序可能会崩溃。

解决方法:在调用WCSession API之前,将NSNumber或NSDate对象转换为string。 在接收方做相反的转换。

观看OS 2 Beta 4发行说明

我的猜测是你的sendMessage函数在失败的情况下返回一个错误,但你还没有实现error handling程序! 现在,当你正在起床和跑步时,只要打印错误就可以逃脱,但如果这是发货代码,你真的应该处理适当的错误:

 // send message session.sendMessage(message, replyHandler: nil, errorHandler: { (error) -> Void in print("Watch send gesture \(gesture) failed with error \(error)") }) print("Watch send gesture \(gesture)") 

你的stream程是正确的,但难点是要了解如何debugging:

debugging观察:

  1. 运行iPhone目标,完成后点击停止button。
  2. 在模拟器中打开iOS应用程序(从模拟器手动运行而不是从Xcode运行),并让它挂在那里。
  3. 切换到Watch目标(yourAppName WatchKit应用程序),放置相关的断点并运行它。
  4. iOS应用程序将自动置于后台,然后您将能够使用sendMessage方法(在Watch目标)发送任何您需要的内容,如果您的iOS应用程序中有replayHandler,您甚至会收到sendMessage在你的Watch目标(即InterfaceController)

小斯威夫特例子:

手表发送字典到iOS应用程序:

  if WCSession.defaultSession().reachable == true { let requestValues = ["Send" : "From iWatch to iPhone"] let session = WCSession.defaultSession() session.sendMessage(requestValues, replyHandler: { (replayDic: [String : AnyObject]) -> Void in print(replayDic["Send"]) }, errorHandler: { (error: NSError) -> Void in print(error.description) }) } else { print("WCSession isn't reachable from iWatch to iPhone") } 

接收来自Watch的消息并从iOS应用程序发送重播:

  func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) { print(message.values) var replyValues = Dictionary<String, AnyObject>() replyValues["Send"] = "Received from iphone" // Using the block to send back a message to the Watch replyHandler(replyValues) } 

debuggingiPhone:

debugging手表完全相反

另外,@sharpBaga的答案有一个重要的考虑。