Search code examples
iosswiftcloudkitwatchos-2

Pass Strings from iPhone


I'm trying to pass the color for each Play found in my CloudKit database.

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

When I NSLog what is getting passed in the iOS side, it logs the 3 colors perfectly.

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

But when I go to the Watch side, nothing shows up in the colorLabel. (I'm not sure if setText is even getting called there because "From iPhone" doesn't even show up.)

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

Any ideas? Can post any extra code as needed, just let me know. Thanks!

EDIT: Per question here is my WCSession code

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!
        }
    }
}

Solution

  • N.B. You pass 'newPlay' in the appContext dictionary, yet print 'newPlayoff' in your log.

    I presume elsewhere you have set the WCSession delegate and called activateSession