使用iOS和WatchKit应用程序,并希望将数据消息从Apple Watch发送到iOS对方。我可以使链接正常工作,但是当我要求电话处理数据时,我什么也没收到。我还尝试过使用调度队列,因为电话在后台。没有didReceiveMessage内部的方法,连接将起作用。也许缺少什么?
此代码位于AppDelegate.swift中,并且会话处于活动状态:
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
DispatchQueue.main.async {
var profile = Profile()
do {
let newProfile = Profile()
profile = newProfile.getProfile()
let completedDate = message["completed"] as? Date
let courseID = message["course"] as? Int
let lessonID = message["lesson"] as? Int
try realm.write {
let lesson = LessonAttempt()
lesson.logLessonAttempt(course: courseID!, lesson: lessonID!, profile: profile, completedDate: completedDate, completion: { finished, error in
if finished == true && error == nil {
replyHandler(["response":true])
} else {
print("Watch log attempt: \(error)")
replyHandler(["response":error?.localizedDescription])
}
})
}
} catch {
print("\(error.localizedDescription)")
replyHandler(["response":error.localizedDescription])
}
}
}
应用会话:
if WCSession.isSupported() {
let session = WCSession.default
session.delegate = self
session.activate()
}
观看应用程序发送消息-错误未返回:
if session.isReachable {
session.sendMessage(data, replyHandler: { replyHandler in
if let response = replyHandler["response"] as? Bool {
print("Response from app to watch: \(response)")
} else {
print("Somethings wrong! \(replyHandler["response"] as! String)")
}
}) { error in
print("Error: \(error.localizedDescription)")
}
}
谢谢