从 iPhone 传递字符串
Pass Strings from iPhone
我正在尝试 为在我的 CloudKit 数据库中找到的每个 Play
传递 color
。
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)
}
}
}
当我 NSLog
在 iOS 端传递的内容时,它 完美地记录了 3 color
s。
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
但是当我转到 Watch 端时,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)")
}
}
}
有什么想法吗?可以根据需要 post 任何额外的代码,请告诉我。谢谢!
编辑:这里的每个问题是我的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!
}
}
}
N.B。您在 appContext 字典中传递 'newPlay',但在您的日志中打印 'newPlayoff'。
我假设您在别处设置了 WCSession 委托并调用了 activateSession
我正在尝试 为在我的 CloudKit 数据库中找到的每个 Play
传递 color
。
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)
}
}
}
当我 NSLog
在 iOS 端传递的内容时,它 完美地记录了 3 color
s。
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
但是当我转到 Watch 端时,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)")
}
}
}
有什么想法吗?可以根据需要 post 任何额外的代码,请告诉我。谢谢!
编辑:这里的每个问题是我的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!
}
}
}
N.B。您在 appContext 字典中传递 'newPlay',但在您的日志中打印 'newPlayoff'。
我假设您在别处设置了 WCSession 委托并调用了 activateSession