如何解决[WC]因缺少委托人而拒绝激活的问题 Swift

问题描述 投票:0回答:1

你好,我有这个功能

extension UIViewController: WCSessionDelegate {
public func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {        
}
public func sessionDidBecomeInactive(_ session: WCSession) {

}
public func sessionDidDeactivate(_ session: WCSession) {
}


//MARK: -SYNC DATA TO APPLE WATCH

func syncToAppleWatch(){
    var session: WCSession?
    if WCSession.isSupported() {//4.1
      session?.delegate = self
      session = WCSession.default//4.2
      session?.activate()//4.4
    }

}

我在DidLoad里面调用 "syncToAppleWatch"。但我收到了这个错误

 [WC] denying activation due to missing delegate
 [WC] WCSession has not been activated

我怎样才能解决这个问题?

swift uiviewcontroller watchconnectivity wcsession
1个回答
1
投票

这两行没有意义

session?.delegate = self  // session is still nil here, delegate won't be set
session = WCSession.default

改为

session = WCSession.default
session?.delegate = self 
© www.soinside.com 2019 - 2024. All rights reserved.