简体   繁体   English

WatchOS WCSession 不会激活/WCsession 缺少它的委托

[英]WatchOS WCSession won't activate / WCsession missing its delegate

Im trying to simply connect an iOS app to a watchOS app with Xcode emulators and using watch connectivity WCSession.我试图简单地将 iOS 应用程序连接到带有 Xcode 模拟器的 watchOS 应用程序并使用手表连接 WCSession。 The watch isn't reachable and session won't activate even though I tried every possible change to the code.手表无法访问,session 将无法激活,即使我已尝试对代码进行所有可能的更改。 I use the same Sync service that I change its target to both watch and iOS apps and I call them as soon as app starts using.didAppear Am I doing something wrong?我使用相同的同步服务,将其目标更改为 watch 和 iOS 应用程序,并在应用程序开始使用时立即调用它们。didAppear 我做错了什么吗?





import Foundation
import WatchConnectivity

class SyncService : NSObject, WCSessionDelegate {
    
    
    private var session: WCSession = .default

    init(session: WCSession = .default) {
        
        
        super.init()
        self.session = session
        self.session.delegate = self
        print("Reachable :",session.isReachable)
        #if os(iOS)
        print("Connection provider initialized on phone")
        #endif
        #if os(watchOS)
        print("Connection provider initialized on watch")
        #endif
        
        self.connect()
        print("Details",self.session)
    }
    
    func connect() {
        guard WCSession.isSupported() else {
            print("WCSession is not supported")
            return
        }
        switch self.session.activationState {
                case .activated:
                    print("WCSession activated successfully")
                case .inactive:
                    print("Unable to activate the WCSession. Error:")
                case .notActivated:
                    print("Unexpected .notActivated state received after trying to activate the WCSession")
                @unknown default:
                    print("Unexpected state received after trying to activate the WCSession")
                }
        session.activate()
        
    }
    
    func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
        
    }

    #if os(iOS)
    func sessionDidBecomeInactive(_ session: WCSession) { }
    
    func sessionDidDeactivate(_ session: WCSession) { }
    #endif
    
        
     //   func sendMessage(_ key: String, _ message: String, _ errorHandler: ((Error) -> Void)?) {
        //    if session.isReachable {
        //        session.sendMessage([key : message], replyHandler: nil) { (error) in
         //           print(error.localizedDescription)
         //           if let errorHandler = errorHandler {
           ///             errorHandler(error)
            //        }
        //        }
       //     }
   //     }
        
    

        
        
    }







import SwiftUI

@main
struct Connectivity_Watch_AppApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView() .onAppear{
                var model = SyncService()
            }
        }
    }
}





import SwiftUI

@main
struct ConnectivityApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView() .onAppear{
                var model = SyncService()
            }
        }
    }
}




You have to wait until session will be activated and only then you can send message.您必须等到 session 被激活,然后您才能发送消息。 So, check there is session is activated and then send message因此,检查是否有 session 已激活,然后发送消息

func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
    if activationState == .activated {
        // send message
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM