我的应用程序的基本概念是,Complication 向 iPhone 请求数据,然后 iPhone 将数据发回。我从我的 iPhone 应用程序中激活了我的 WCSession didFinishLaunchingWithOptions
。同样在我的 Watch 应用程序中,我已经覆盖init()
了 ExtensionDelegate 中的。
这是我在 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 中,当我激活我的 Complication 时应该调用它,我调用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
没有didReceiveUserInfo
当我在 XCode 中的模拟器上调试复杂功能时。