我正在尝试为在我的color
数据库中找到的每个 Play
传递。
iPhone -->手表
iOS TableViewController
func getCloudKit() {
///...
let publicData = container.publicCloudDatabase
publicData.performQuery(query, inZoneWithID: nil) { results, error in
if error == nil { // There is no error
for play in results! {
let newPlay = Play()
do {
try WatchSessionManager.sharedManager.updateApplicationContext(["color" : newPlay.teamColor])
NSLog("NewPColor: %@", newPlay.teamColor)
} catch {
print(error)
}
self.objects.append(newPlay)
}
} else {
print(error)
}
}
}
当我在color
**s**端传递什么时,会完美地记录3的内容。
2015-09-25 21:10:28.706 Play[16444] NewPColor: FDB927
2015-09-25 21:10:28.707 Play[16444] NewPColor: 000000
2015-09-25 21:10:28.708 Play[16444] NewPColor: 000000
但是当我去看colorLabel
.的时候,什么都没有出现在(我不确定setText
是否会被打电话到那里,因为"From iPhone“甚至没有出现。)
WatchKit InterfaceController
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
let colorWatch = applicationContext["color"] as? String
dispatch_async(dispatch_get_main_queue()) {
if let colorWatch = colorWatch {
self.colorLabel.setText("From iPhone: \(colorWatch)")
}
}
}
有什么想法吗?可以根据需要发布任何额外的代码,请告诉我。谢谢!
编辑:每个问题的在这里是我的WCSession
代码
iOS WatchSessionManager
class WatchSessionManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
private var validSession: WCSession? {
if let session = session where session.paired && session.watchAppInstalled {
return session
}
return nil
}
func startSession() {
session?.delegate = self
session?.activateSession()
}
}
extension WatchSessionManager {
// Sender
func updateApplicationContext(applicationContext: [String : AnyObject]) throws {
if let session = validSession {
do {
try session.updateApplicationContext(applicationContext)
} catch let error {
throw error
}
}
}
// Receiver
func session(session: WCSession, didReceiveApplicationContext applicationContext: [String : AnyObject]) {
// handle receiving application context
dispatch_async(dispatch_get_main_queue()) {
// make sure to put on the main queue to update UI!
}
}
}
发布于 2015-09-26 03:38:17
注:你在newPlay字典中传递'newPlayoff‘,而在日志中打印’newPlayoff‘。
我猜想在其他地方您已经设置了WCSession委托并调用了activateSession
https://stackoverflow.com/questions/32772756
复制相似问题