首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从iPhone传递字符串

从iPhone传递字符串
EN

Stack Overflow用户
提问于 2015-09-24 23:56:07
回答 1查看 103关注 0票数 0

我正在尝试为在我的color数据库中找到的每个 Play传递

iPhone -->手表

iOS TableViewController

代码语言:javascript
运行
复制
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的内容。

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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

代码语言:javascript
运行
复制
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!
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-09-26 03:38:17

注:你在newPlay字典中传递'newPlayoff‘,而在日志中打印’newPlayoff‘。

我猜想在其他地方您已经设置了WCSession委托并调用了activateSession

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32772756

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档