所以我正在使用 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 上的任何内容都没有被执行。即使我手动打开应用程序。