所以我使用Watch Connectivity来请求从iPhone到Watch的阵列。
想法来自手表sendMessage
,iPhone将使用didReceiveMessage
方法回复数组。
然而,iPhone似乎没有响应,我认为iPhone会在我从Watch发送消息时打开应用程序。我尝试在sendMessage
时打开应用程序,但仍然没有运气。当我等待足够长时间时,我收到以下错误消息:
错误域= WCErrorDomain代码= 7012"邮件回复花费的时间太长。" UserInfo = {NSLocalizedDescription =邮件回复花了太长时间。, NSLocalizedFailureReason =发生回复超时。}
有人知道我哪里出错吗?
Apple Watch
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已成为可能 数组没有
iPhone App代理
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的任何内容。即使我手动打开应用程序。
答案 0 :(得分: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])
如果使用非零 replyHandler调用sendMessage,则将在接收方调用此委托方法:
func session(session: WCSession, didReceiveMessage message: [String : AnyObject], replyHandler: ([String : AnyObject]) -> Void)
答案 1 :(得分: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应用程序保持在前台。