The basic concept of my App is that the Complication requests the iPhone for data and the iPhone then sends the data back.
I activate my WCSession from my iPhone App in the didFinishLaunchingWithOptions
. Also in my Watch app I've overwritten the init()
from the ExtensionDelegate.
This is my singleton from the iPhone:
class WatchConnectivityManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchConnectivityManager()
private override init() {
super.init()
}
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
func startSession() {
print("session started")
session?.delegate = self
session?.activateSession()
}
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
print("didReceiveUserInfo")
session.transferCurrentComplicationUserInfo(["reply":""])
}
}
And this is my singleton from my Watch:
class WatchConnectivityManager: NSObject, WCSessionDelegate {
static let sharedManager = WatchConnectivityManager()
private override init() {
super.init()
}
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
func startSession() {
print("session started")
session?.delegate = self
session?.activateSession()
}
func session(session: WCSession, didReceiveUserInfo userInfo: [String : AnyObject]) {
print("didReceiveUserInfo")
}
func requestData() {
print("data requested")
session!.transferUserInfo(["request" : ""])
}
}
In my ExtensionDelegate's init, which should be called when I activate my Complication I call the startSession
and the requestData
:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
override init() {
WatchConnectivityManager.sharedManager.startSession()
WatchConnectivityManager.sharedManager.requestData()
}
func applicationDidFinishLaunching() {}
func applicationDidBecomeActive() {}
func applicationWillResignActive() {}
}
And the AppDelegate from my iPhone:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
WatchConnectivityManager.sharedManager.startSession()
return true
}
The output is simply:
session started
data requested
without the didReceiveUserInfo
when I debug the Complication on a Simulator in XCode.