传递字符串从iPhone

问题描述:

我试图通过color为每个Play发现我的CloudKit数据库。传递字符串从iPhone

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小号完美。

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甚至获取调用那里,因为 “从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委托,并呼吁activateSession

+0

遗憾的是第一部分是复制和粘贴的mixup。而对于'WCSession'代码我确实有这个,我发布它只是为了防止你想看看它。有任何想法吗?谢谢! – SRMR