Hi folks, I'm attempting to add WKConnectivity to a SwiftUI app. When I try to introduce the PhoneConnect class to my main view, the error occurred. How can I fix this issue? Here is the code:
struct HomeView: View {
@State var Listen = false
@State var showAlert = false
@State var reach = "No"
@ObservedObject var connect = PhoneConnect() //Cannot find 'PhoneConnect' in scope
import WatchConnectivity
final class MessageViewModel: NSObject, ObservableObject {
@Published var messages: [String] = []
@Published var messagesData: [String] = []
var session: WCSession
init(session: WCSession = .default) {
self.session = session
super.init()
self.session.delegate = self
session.activate()
}
}
extension MessageViewModel: WCSessionDelegate {
func sessionDidBecomeInactive(_ session: WCSession) {
}
func sessionDidDeactivate(_ session: WCSession) {
}
func session(_ Session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
if let error = error {
print(error.localizedDescription)
} else {
print("The session has completed activation.")
}
}
//[String: Any]
func session(_ session: WCSession, didReceiveMessage message: [String : Any]) {
DispatchQueue.main.async {
let receivedState = message["State"] as? String ?? "Syncing"
print(receivedState)
self.messages.append(receivedState)
}
}
}