6

所以我正在使用 Watch Connectivity 从 iPhone 向 Watch 请求一个数组。

这个想法是sendMessage从手表开始,iPhone 会用didReceiveMessage方法中的数组进行回复。

但是 iPhone 似乎没有响应,我认为当我从 Watch 发送消息时 iPhone 会打开应用程序。我什至尝试过打开应用程序,sendMessage但仍然没有运气。当我等待足够长的时间时,我收到以下错误消息:

错误域 = WCErrorDomain 代码 = 7012 “消息回复时间过长。” UserInfo={NSLocalizedDescription=消息回复时间过长。,NSLocalizedFailureReason=回复超时。}

有人知道我哪里可能出错了吗?

苹果手表

import WatchKit
import Foundation
import CoreData
import WatchConnectivity

class BookmarkedInterfaceController: WKInterfaceController, WCSessionDelegate {

var session : WCSession!
var objects: [AnyObject]!

@IBOutlet var table: WKInterfaceTable!

override func willActivate() {
    super.willActivate()
    //Check if session is supported and Activate
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
    sendMessageToIphone()
}
func sendMessageToIphone() {
    if WCSession.defaultSession().reachable {

        print("WCSession is reachabe")

        let messageDict = ["Request": "iPhone Can You Give Me The Array"]
        WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
            print(replyDict)
            }, errorHandler: { (error) -> Void in
                print(error)
        })
    }
}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
   //recieving message from iphone
    print("recieved message from iphone \(message)")

    objects.append(message["Array"]!)

    print("Objects array = \(objects)")        
}

控制台输出

WCSession 是可达数组 nil

iPhone 应用程序代表

import UIKit
import CoreData
import WatchConnectivity

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?
var session : WCSession!

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {

    //Check if session is supported and Activate
    if (WCSession.isSupported()) {
        session = WCSession.defaultSession()
        session.delegate = self
        session.activateSession()
    }
}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {

    print("did recieve message from Watch")

    let applicationData = ["Array":["One", "Two", "Three"]]

    replyHandler(applicationData)

}

iPhone 上的任何内容都没有被执行。即使我手动打开应用程序。

4

2 回答 2

3

如果您希望对手表发送的消息的回复包含所请求的数据,则应将代码更改为以下内容:

手表

func sendMessageToIphone() {
    if WCSession.defaultSession().reachable {

        print("WCSession is reachabe")

        let messageDict = ["Request": "iPhone Can You Give Me The Array"]
        WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
            print("Array \(replyDict["array"])")
            }, errorHandler: { (error) -> Void in
                print(error)
        })
    }
}

电话

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {

    print("did recieve message from Watch")

    let applicationData = ["Array":["One", "Two", "Three"]]

    //If identifier from recievedMessage is for Objects

    replyHandler(applicationData)

}

另外,手表没有收到来自手机的 sendMessage 的原因是因为您为正在使用的 sendMessage 调用实现了错误的委托方法。

如果您使用nil replyHandler 调用 sendMessage ,则将在接收端调用此委托方法: func session(session: WCSession, didReceiveMessage message: [String : AnyObject])

如果您使用非 nil replyHandler 调用 sendMessage ,则将在接收端调用此委托方法: func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)

于 2016-02-03T06:41:14.773 回答
0

您必须在发送消息之前激活会话。此外,您必须在激活会话之前设置委托,因为您可能会丢失一些待处理的消息。

iphone端:

import UIKit
import WatchConnectivity

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {

var window: UIWindow?
var session : WCSession!

func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void) {

    print("did recieve message from Watch")
    let applicationData = ["Array":["One", "Two", "Three"]]

    //If identifier from recievedMessage is for Objects
    session.sendMessage(applicationData, replyHandler: { reply in
        print("Got reply: \(reply)")
        }, errorHandler: { error in
            print("error: \(error)")
    })
}

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
    session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()
    return true
}
}

iWatch 扩展 InterfaceController 您必须在接口控制器 willactivate 方法中激活会话。

import WatchKit
import Foundation
import WatchConnectivity

class InterfaceController: WKInterfaceController, WCSessionDelegate {

var session : WCSession!
var objects: [AnyObject]!

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

    // Configure interface objects here.
}
override func willActivate() {
    // This method is called when watch view controller is about to be visible to user
    super.willActivate()
    session = WCSession.defaultSession()
    session.delegate = self
    session.activateSession()
    objects = []
    sendMessageToIphone()
}

override func didDeactivate() {
    // This method is called when watch view controller is no longer visible
    super.didDeactivate()
}

func sendMessageToIphone() {
    if WCSession.defaultSession().reachable{

        print("WCSession is reachabe")

        let messageDict = ["Request": "iPhone Can You Give Me The Array"]
        WCSession.defaultSession().sendMessage(messageDict, replyHandler: { (replyDict) -> Void in
            print(replyDict)
            }, errorHandler: { (error) -> Void in
                print(error)
        })
    }
}

func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
    //recieving message from iphone
    print("recieved message from iphone \(message)")

    objects.append(message["Array"]!)

    print("Objects array = \(objects)")
}
}

笔记。首先运行 iphone 应用程序。然后运行扩展程序并将 iphone 应用程序保持在前台。

于 2016-02-05T11:18:22.720 回答