从Watch到App发送字典 - WatchConnectivity

时间:2015-10-23 13:31:40

标签: ios swift dictionary watchkit

我有一本字典

dict = [Int : Bool]

我想知道是否有一种简单的方法可以通过WatchConnectivity框架中的交互式消息传递从观看 iPhone ,反之亦然?

谢谢。

2 个答案:

答案 0 :(得分:1)

你可以这样做:

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

    let dict = [Int : bool]
    let message = ["message" : dict]
    WCSession.default.sendMessage(message,
        replyHandler: { (reply) -> Void in
        },
        errorHandler: { (error) -> Void in
        }
    )

答案 1 :(得分:0)

watchkit extention编写以下代码:

func getDataFromParentApp(image: String) {
    let dictionary = ["Desired Word":image]
    WKInterfaceController.openParentApplication(dictionary) {
        (replyInfo, error) -> Void in
    }

AppDelegate写下一个:

func application(application: UIApplication!,
    handleWatchKitExtensionRequest userInfo: [NSObject : AnyObject]!,
    reply: (([NSObject : AnyObject]!) -> Void)!) {
        getImageForWordThatRhymesWithDat(userInfo, reply)
}

您可以参考此link