Post

Replies

Boosts

Views

Activity

Reply to iOS14: How to using delegate like WCSessionDelegate without NSObject?
I found a way: import SwiftUI import WatchConnectivity struct WatchInfo: View {   @State private var showMessage: String = "Wating"       var body: some View {     VStack {       Text(showMessage).onAppear() {         ABWatchSessionManager.sharedInstance.addDelegateObject(WatchSessionDelegate(self))       }     }   }       class WatchSessionDelegate: NSObject, WCSessionDelegate {     var parent: WatchInfo     init(_ watchInfo: WatchInfo) {       self.parent = watchInfo     }           func session(_ session: WCSession, didReceiveMessage message: [String : Any], replyHandler: @escaping ([String : Any]) -> Void) {       let data: Dictionary<String, String> = message["data"] as! Dictionary<String, String>       if data["dataType"] == DataType.ping.rawValue {         parent.showMessage = "Get PING"       } else if data["dataType"] == DataType.data.rawValue {         parent.showMessage = "Get DATA"       }     }           func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {     }           func session(_ session: WCSession, didReceiveApplicationContext applicationContext: [String : Any]) {     }   } } struct WatchInfo_Previews: PreviewProvider {   static var previews: some View {     WatchInfo()   } }
Jul ’20
Reply to iOS14: How to using delegate like WCSessionDelegate without NSObject?
The other part of my code: import Foundation import WatchConnectivity typealias WeakDelegate = () -> WCSessionDelegate? func makeWeakDelegate(_ delegate: WCSessionDelegate?) -> WeakDelegate? {   //weak var weakDelegate: WCSessionDelegate? = delegate   return {     return delegate   } } func weakDelegateObject(_ weakDelegate: WeakDelegate?) -> WCSessionDelegate? {   return weakDelegate?() as WCSessionDelegate? } class ABWatchSessionManager: NSObject {       private(set) var delegates: [WeakDelegate]? = []       private(set) var session: WCSession = WCSession.default       static let sharedInstance = ABWatchSessionManager()       private override init() {     super.init()   }       func startSession() {     if !WCSession.isSupported() {       return     }     self.session.delegate = self;     self.session.activate()   }       #if os(iOS)   func isValidSession() -> Bool {     if self.session.isPaired && self.session.isWatchAppInstalled {       return true     } else {       return false     }   }   #endif       func addDelegateObject(_ delegate: WCSessionDelegate?) {     let abc = makeWeakDelegate(delegate)!     self.delegates?.append(abc)   }       func removeDelegateObject(_ delegate: WCSessionDelegate?) {     self.delegates?.removeAll { $0 as AnyObject === makeWeakDelegate(delegate) as AnyObject }   }       func sendMessage(_ message: [String : Any], replyHandler: (([String : Any]) -> Void)?, errorHandler: ((Error) -> Void)? = nil) {     #if os(iOS)     if !self.isValidSession() {       return     }     #endif           if self.session.isReachable {       self.session.sendMessage(message, replyHandler: replyHandler, errorHandler: errorHandler)     } else {       do {         try self.session.updateApplicationContext(message)       } catch (let error) {         print(error)       }     }   }       func sendMessageData(_ data: Data, replyHandler: ((Data) -> Void)?, errorHandler: ((Error) -> Void)? = nil) {     #if os(iOS)     if !self.isValidSession() {       return     }     #endif           if self.session.isReachable {       self.session.sendMessageData(data, replyHandler: replyHandler, errorHandler: errorHandler)       return     }   } } extension ABWatchSessionManager: WCSessionDelegate {       // #if IPHONE_9_3 __WATCHOS_2_2   func session(_ session: WCSession, activationDidCompleteWith activationState: WCSessionActivationState, error: Error?) {     self.delegates?.forEach({ (delegate) in       weakDelegateObject(delegate)?.session(session, activationDidCompleteWith: activationState, error: error)     })   }       // #if TARGET_OS_IOS && __IPHONE_9_3   #if os(iOS)   func sessionDidBecomeInactive(_ session: WCSession) {     self.delegates?.forEach({ (delegate) in       weakDelegateObject(delegate)?.sessionDidBecomeInactive(session)     })   }       func sessionDidDeactivate(_ session: WCSession) {     self.delegates?.forEach({ (delegate) in       weakDelegateObject(delegate)?.sessionDidDeactivate(session)     })   } ...... }
Jul ’20
Reply to SwiftUI Tutorial (Combining Views) - Map Pin missing
// // MapView.swift // Landmark // // Created by Abenx on 2020/6/26. // import SwiftUI import MapKit #if !os(macOS) struct MapView: UIViewRepresentable {   var coordinate: CLLocationCoordinate2D   func makeUIView(context: Context) -> MKMapView {     MKMapView(frame: .zero)   }       func updateUIView( uiView: MKMapView, context: Context) {     self.updateView(uiView, context: context)   } } #else struct MapView: NSViewRepresentable {   var coordinate: CLLocationCoordinate2D   func makeNSView(context: Context) -> MKMapView {     MKMapView(frame: .zero)   }       func updateNSView( nsView: MKMapView, context: Context) {     self.updateView(nsView, context: context)   } } #endif extension MapView {   func updateView( uiView: MKMapView, context: Context) {     let span = MKCoordinateSpan(latitudeDelta: 0.01, longitudeDelta: 0.01)     let region = MKCoordinateRegion(center: coordinate, span: span)     let annotation = MKPointAnnotation()     annotation.coordinate = coordinate           uiView.setRegion(region, animated: true)     uiView.addAnnotation(annotation)   } }     struct MapViewPreviews: PreviewProvider {   static var previews: some View {     MapView(coordinate: CLLocationCoordinate2D(           latitude: 39.910358, longitude: 116.469841))   } }
Jun ’20