SwiftUI에서 @EnvironmentObejct는 View에서만 사용가능합니다.
import Foundation
import WatchConnectivity
import SwiftUI
class WatchToiOS: NSObject, ObservableObject {
@EnvironmentObject var workoutManager: WorkoutManager
@Published var startStatus: Bool = false
let session = WCSession.default
override init() {
super.init()
if WCSession.isSupported() {
session.delegate = self
session.activate()
}
}
func sendMessage(message: [String:Any]) {
session.sendMessage(message) { _ in
}
}
}
extension WatchToiOS: WCSessionDelegate {
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
switch activationState {
case .activated:
print("Watch: WCSession activated successfully")
case .inactive:
print("Watch: Unable to activate the WCSession. Error: \(error?.localizedDescription ?? "--")")
case .notActivated:
print("Watch: Unexpected .notActivated state received after trying to activate the WCSession")
@unknown default:
print("Watch: Unexpected state received after trying to activate the WCSession")
}
}
func session(_ session: WCSession, didReceive file: WCSessionFile) {
}
func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {
if let getHeartRate = message["heart"] {
if getHeartRate as? Bool == true {
print(message.description)
replyHandler(["heartRate":self.workoutManager.heartRate])
}
}
}
}
'SwiftUI > 에러해결' 카테고리의 다른 글
SwiftUI - Observable 여러 뷰에서 데이터 업데이트 안됨 (0) | 2024.08.21 |
---|---|
SwiftUI - ChatGPT 에 이미지 전송 안됨 (0) | 2024.08.10 |
PhotosPicker 사진 회전되어 출력되는 현상 해결 (0) | 2024.07.10 |
SwiftData Fatal Error in ModelContainer.swift 에러 (0) | 2024.06.22 |
JSONDecoder로 Decode할 때 에러 (0) | 2024.05.08 |