gpt4 book ai didi

ios - 从 iPhone 传递字符串

转载 作者:行者123 更新时间:2023-11-29 01:37:48 26 4
gpt4 key购买 nike

我正在尝试传递在我的 CloudKit 数据库中找到的每个 Playcolor

iPhone --> watch

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个颜色

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)")
}
}
}

有什么想法吗?可以根据需要发布任何额外的代码,请告诉我。谢谢!

编辑:这里的每个问题都是我的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!
}
}
}

最佳答案

注意您在 appContext 字典中传递“newPlay”,但在您的日志中打印“newPlayoff”。

我假设您在别处设置了 WCSession 委托(delegate)并调用了 activateSession

关于ios - 从 iPhone 传递字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32772756/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com