Post

Replies

Boosts

Views

Activity

Reply to Updating Time - SwiftUI
you could use something like this: import SwiftUI struct TestView: View {       @State var timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect()   @State var timeNow = ""   let dateFormatter = DateFormatter()        var body: some View {     Text("Currently: " + timeNow)       .onReceive(timer) { _ in         self.timeNow = dateFormatter.string(from: Date())       }       .onAppear(perform: {dateFormatter.dateFormat = "LLLL dd, hh:mm:ss a"})   } } This will display the time updated every second
Aug ’20
Reply to Updating Time - SwiftUI
I didn't know it sometimes does not work. I'm using xcode 12.0-beta-3, target ios 13.5 and 14, tested on a few simulators and real devices, ipad(ios 14) and iphone(ios 13.5). I cannot see any problems with the following (updated) test: import SwiftUI struct ContentView: View { 		var body: some View { 				TestView().padding() 		} } struct TestView: View { 		@State var timeNow = "" 		let timer = Timer.publish(every: 1, on: .main, in: .common).autoconnect() 		var dateFormatter: DateFormatter { 				let fmtr = DateFormatter() 				fmtr.dateFormat = "LLLL dd, hh:mm:ss a" 				return fmtr 		} 		 		var body: some View { 				Text("Currently: " + timeNow) 						.onReceive(timer) { _ in 								self.timeNow = dateFormatter.string(from: Date()) 						} 		} } What setup does it fail on?
Aug ’20
Reply to What's the best way to know when a user is editing a SecureField in SwiftUI?
you could try something like this: &#9;&#9;&#9;&#9;&#9;&#9;@State var theText = "" &#9;&#9;&#9;&#9;&#9;&#9;@State var isInSecureField = false &#9;&#9;&#9;&#9;&#9;&#9;SecureField("enter your password", text: Binding<String>( &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;get: { self.theText }, &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;set: { self.theText = $0 &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;print("editing a SecureField") self.isInSecureField = true&#9;&#9; &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; } &#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9; ))
Sep ’20
Reply to Pair this http post code with a button in Xcode
What errors do you get when you create a new project? What system are you using? You can do the following to setup a button to send a request. Note: you need to use "https" otherwise you will have to fiddle with the security entitlements. import SwiftUI &#9; struct ContentView: View { &#9;&#9;var body: some View { &#9;&#9; Button(action: {sendHttpPost()}) { &#9;&#9;&#9;&#9;Text("Press here to send a post") .padding(15) .foregroundColor(.blue) .border(Color.blue, width: 2) }.buttonStyle(PlainButtonStyle()) } func sendHttpPost() { let session = URLSession(configuration: .default) let url = URL(string: "http:...//ServerIP:8000/xmlcommand")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("text/plain", forHTTPHeaderField: "Content-Type") // I guess this can be "text/xml" // Working line request.httpBody = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Commands><command type=\"open\"><name>environments/Animals.hwe</name><id>a1</id></command></Commands>".data(using: .utf8) let task = session.dataTask(with: request) { data, response, error in // print(data as Any) // print(response as Any) // print(error as Any) // do something with the result print(data as Any? as Any) if let data = data { print(String(data: data, encoding: .utf8)!) } else { print("no data") } } task.resume() // <- otherwise your network request won't be started } }
Nov ’20