new: watchOS watch end app

This commit is contained in:
lollipopkit
2023-09-16 17:53:34 +08:00
parent f74c5cd9ba
commit ef880c67af
5 changed files with 466 additions and 0 deletions

View File

@@ -0,0 +1,54 @@
//
// PhoneConnMgr.swift
// WatchEnd Watch App
//
// Created by lolli on 2023/9/16.
//
import WatchConnectivity
class PhoneConnMgr: NSObject, WCSessionDelegate, ObservableObject {
var session: WCSession?
var _ctx: [String: Any] = [:]
var ctx: [String: Any] {
set {
Store.setCtx(newValue)
updateUrls(newValue)
}
get {
return _ctx
}
}
@Published var urls: [String] = []
override init() {
super.init()
if !WCSession.isSupported() {
print("WCSession not supported")
}
session = WCSession.default
session?.delegate = self
session?.activate()
ctx = Store.getCtx()
print("init", ctx)
}
func updateUrls(_ val: [String: Any]) {
if let urls = val["urls"] as? [String] {
self.urls = urls.filter { !$0.isEmpty }
}
}
func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {
}
// implement session:didReceiveApplicationContext:
func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {
ctx = applicationContext
print("update", ctx)
}
}