WCSession.sendMessage工作50/50 [英] WCSession.sendMessage works 50/50

查看:319
本文介绍了WCSession.sendMessage工作50/50的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我正在开发一个与Watch/ iPhone通信相关的项目。但是我的代码有时工作并且有时不起作用,这对我来说有点奇怪,因为我认为代码应该工作与否。它不能是50/50。因此,我不知道出了什么问题。

Lately, I am working on a project is related to Watch/iPhone communication again. But my code works sometimes and doesn’t work sometimes which is kind of weird to me because I think the code should either work or not. It cannot be 50/50. Therefore, I have no idea what goes wrong.

在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){

func sendGesture(gesture : 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上调试我的Watch应用程序,调试会话就不会附加。我不知道为什么。因此,我只用iPhone进行单面调试。

somehow I can’t debug my Watch app on Xcode, the debug session just won’t attach. I don’t know why. Therefore, I debug one-sided with just the iPhone.

有时我会打印出接收手势,有时也不会。并且获取通知也是如此。

sometimes I got "receives gesture" print out, and sometimes not. And the same for getting the notification.

推荐答案

我不知道在WCSession中传输时是否会将Int包裹到NSNumber。如果是,那么必须是为什么当我使用Int作为枚举的基类时,它将无法工作,并且当String是基类时可以工作。

I don't know if Int would be wrapped around to NSNumber while being transfer within WCSession. If it would be, then that must be why when I use Int as the base class of the enum it won't work and works when String is the base class.


连接已知问题当使用带有WCSession API的NSNumber和
NSDate对象时,您的应用可能会崩溃。

Connectivity Known Issue Your app may crash when using NSNumber and NSDate objects with the WCSession API.

解决方法:在$之前将NSNumber或NSDate对象转换为字符串b $ b调用WCSession API。在接收
方面进行相反的转换。

Workaround: Convert an NSNumber or NSDate object to a string before calling WCSession APIs. Do the opposite conversion on the receiving side.

观看OS 2 Beta 4发行说明

这篇关于WCSession.sendMessage工作50/50的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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