我正在尝试在iOS7上运行此代码,但它失败了
@available(iOS 9.0, *)
class WatchConnection: NSObject, WCSessionDelegate {
class func sharedInstance() -> WatchConnection {
struct WatchConnectionSingleton {
static let instance = WatchConnection()
}
return WatchConnectionSingleton.instance
}
override init() {
super.init()
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
但这有效
import WatchKit
import WatchConnectivity
class WatchConnection: NSObject, WCSessionDelegate {
class func sharedInstance() -> WatchConnection {
struct WatchConnectionSingleton {
static let instance = WatchConnection()
}
return WatchConnectionSingleton.instance
}
override init() {
super.init()
guard #available(iOS 9.0, *) else { return }
if (WCSession.isSupported()) {
let session = WCSession.defaultSession()
session.delegate = self
session.activateSession()
}
}
// use with invalidated session
@available(iOS 9.0, *)
func getUserId(completion: ((String?) -> Void)?) {
答案 0 :(得分:4)