我的App的基本概念是Complication向iPhone请求数据,然后iPhone将数据发回。
我在didFinishLaunchingWithOptions
的iPhone应用程序中激活了我的WCSession。另外在我的Watch应用程序中,我已经覆盖了ExtensionDelegate中的init()
。
这是我的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":""])
}
}
这是我手表中的单身人士:
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" : ""])
}
}
在我的ExtensionDelegate的init中,当我激活我的复杂功能时应调用它,我调用startSession
和requestData
:
class ExtensionDelegate: NSObject, WKExtensionDelegate {
override init() {
WatchConnectivityManager.sharedManager.startSession()
WatchConnectivityManager.sharedManager.requestData()
}
func applicationDidFinishLaunching() {}
func applicationDidBecomeActive() {}
func applicationWillResignActive() {}
}
来自我iPhone的AppDelegate:
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
WatchConnectivityManager.sharedManager.startSession()
return true
}
输出只是:
session started
data requested
当我在XCode中的模拟器上调试复杂时,没有didReceiveUserInfo
。