I've been using NatashaTheRobot singleton for the WCSession, but can't get the sendMessage to work properly.
My goal is to send a message from Watch app to the iOS app and transfer a dictionary from iOS app to watch app.
Here's my code in the ExtensionDelegate
import WatchKit
import WatchConnectivity
class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
var session:WCSession!
var boolCheck = Int()
func applicationDidFinishLaunching() {
WatchSessionManager.sharedManager.startSession()
print("Here i am")
}
func applicationDidBecomeActive() {
print("I AWOKE")
}
func applicationWillResignActive() {
}
}
class WatchSessionManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchSessionManager()
private override init() {
super.init()
}
private let session: WCSession = WCSession.defaultSession()
func startSession() {
session.delegate = self
session.activateSession()
if WCSession.isSupported(){
self.session.sendMessage(["b":"peek"], replyHandler: nil, errorHandler: nil)
print("works")
} else {
print("don't work")
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
let sweetN = message["b"]! as? String
dispatch_async(dispatch_get_main_queue(), {
if sweetN == "insertData1" {
NSNotificationCenter.defaultCenter().postNotificationName("sweetData1", object: nil)
})
}
func sendMessage(message: [String : AnyObject],
replyHandler: (([String : AnyObject]) -> Void)? = nil,
errorHandler: ((NSError) -> Void)? = nil)
{
session.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler)
print("this is message \(replyHandler)")
var pretty = replyHandler
}
Here's my code in the WCSingleton in the iOS app (separate from the AppDelegate)
import WatchConnectivity
@available(iOS 9.0, *)
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()
}
func session(session: WCSession, didReceiveMessage message: [String : AnyObject]) {
//receieve messages from watch
print(message["b"]! as? String)
let sweetN = message["b"]! as? String
dispatch_async(dispatch_get_main_queue(), {
if sweetN == "peek"{
NSNotificationCenter.defaultCenter().postNotificationName("giveMeInfo", object: nil)
}
}
})
}
@available(iOS 9.0, *)
extension WatchSessionManager {
func sendMessage(message: [String : AnyObject],
replyHandler: (([String : AnyObject]) -> Void)? = nil,
errorHandler: ((NSError) -> Void)? = nil)
{
session!.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler)
}
}
and here's the method i use in the ViewController (fired from NSNotificationCenter
). However this part of code never gets executed (which is strange, because when i use applicationContext it works perfectly).
func giveMeInfo(){
let linesAdd1 = linesAdd as! AnyObject
WatchSessionManager.sharedManager.sendMessage(["a":linesAdd1])
}
Any insights of how to get all those parts working together are very welcome!