Post

Replies

Boosts

Views

Activity

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
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 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 How to take a screenshot of List in a ScrollView of SwiftUI?
Lucky, I found a way to take screenshot in scrollview ... struct ColorGroupView: View {   var colors: [UIColor]   @State var coverImage: UIImage   var body: some View {     let content =       VStack {         ForEach(1...50, id: \.self) { i in           Text("Hello \(i)")             .frame(maxWidth: .infinity, minHeight: 60)         }     }           return GeometryReader { geometry in       VStack(spacing: 0) {         ScrollView {           content         }         ScrollView {           Image(uiImage: self.coverImage)             .frame(maxWidth: .infinity, minHeight: 60)         }         HStack{           Button("Button") {             let screenshot = content.takeScreenshot(origin: geometry.frame(in: .global).origin, size: geometry.size)             self.coverImage = screenshot           }         }         .padding()         .padding(.bottom, geometry.safeAreaInsets.bottom)         .frame(maxWidth: .infinity)         .background(VisualEffectBlur())       }       .edgesIgnoringSafeArea(.bottom)     }   } } extension View {   func takeScreenshot(origin: CGPoint, size: CGSize) -> UIImage {     let window = UIWindow(frame: CGRect(origin: origin, size: size))     let hosting = UIHostingController(rootView: self)           hosting.view.sizeToFit()           window.height = hosting.view.frame.size.height > 0 ? hosting.view.frame.size.height : window.height     window.width = hosting.view.frame.size.height > 0 ? hosting.view.frame.size.height : window.width           window.rootViewController = hosting     window.makeKeyAndVisible()     return window.snapshotImage()!   } } ... extension UIView { func height ... func width ... func snapshotImage ...
Jul ’20
Reply to NSPersistentCloudKitContainer for Shares
WWDC20 did it! let containerIdentifier = persistentStoreDescriptions.cloudKitContainerOptions!.containerIdentifier persistentStoreDescriptions.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: containerIdentifier) persistentStoreDescriptions.cloudKitContainerOptions?.databaseScope = .public       But, when could we really using it in apps?
Jul ’20