当手表停用并快速重新激活(在1-2秒的时间范围内降低和升高)时,WatchConnectivity似乎无法更新其可达状态。这在我的设备上可靠地发生。
编辑:以下是该错误的演示:https://youtu.be/hx7VR8IPxT8
这是一个开放式雷达报告#23337218
我编写了一个测试项目,为您提供最简单的错误示例。
import UIKit
import WatchConnectivity
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, WCSessionDelegate {
var window: UIWindow?
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
session?.delegate = self;
session?.activateSession()
return true
}
func sessionReachabilityDidChange(session: WCSession) {
print("Session \(session.reachable ? "reachable" : "not reachable")")
dispatch_async(dispatch_get_main_queue(),{
//updates UI
})
}
}
手表扩展
import WatchKit
import WatchConnectivity
class ExtensionDelegate: NSObject, WKExtensionDelegate, WCSessionDelegate {
private let session: WCSession? = WCSession.isSupported() ? WCSession.defaultSession() : nil
func applicationDidFinishLaunching() {
// Perform any final initialization of your application.
}
func applicationDidBecomeActive() {
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
session?.delegate = self;
session?.activateSession()
}
func applicationWillResignActive() {
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, etc.
}
}
当我降低监视时会发生什么:委托在1-2秒内调用sessionReachabilityDidChange给出正确的状态。
THEN 如果我在sessionReachabilityDidChange更新后引发监视,则会按预期再次调用委托。
但是如果我在sessionReachabilityDidChange更新之前提升监视,则委托只会被调用一次,反映NOT可达状态并且无法再次调用以反映监视应用程序的正确活动状态。
下载到项目https://dl.dropboxusercontent.com/u/2649851/WatchConnectivityTest.zip
编辑:我应该提到这似乎不是模拟器上的问题。