Posts

Post marked as solved
10 Replies
1.5k Views
Hi Dev Forum I have this piece of SwiftUI code that present an Input field for the user on iOS, Mac to enter an IP address, the entered IP I want to save in a variable so it can be inserted in my further http requests to a server API but it seems like I'm not doing it right. import SwiftUI // Input of Controller IP struct ServerIPInput: View {     @State var CTRLIP: String = ""     var body: some View {         VStack(alignment: .leading) {             Text("Enter Control IP")                 .font(.callout)                 .bold()             TextField("Enter Control IP...", text: $CTRLIP)                 .textFieldStyle(RoundedBorderTextFieldStyle())         }.padding()     }   } I then have this code somewhere else in same file, but this is in a new "struct" let servIp = "$CTRLIP"     // here I would need this entered IP   How can I make the user entered "CTRLIP" equal to my let statement for servIp? Any help with this would be great. Thanks Me
Posted
by Enevold.
Last updated
.
Post not yet marked as solved
0 Replies
499 Views
Hi Forums Is it possible in SwiftUI to read simple XML output from a server like this below, that then can be changed into a list of "swipe" buttons/navigations in iOS or Mac, where if I swipe to the right it shows a Blue-"Open" and if I Swipe left It shows a Red-"Close" a bit like it is in the iOS "Mail" App. The Open and Close I in general need to pass on for other code to open or close this particular Environment. The list should only show the names of each object seen in the XML, aka like Animals.hwe I want also to strip out .hwe and only show the name e.g. Animals, I have first of all been tinkering with Hstack and Vstack, but I do not get something like I would want it to be so I'm a bit lost at the moment, anyone have some snippets that does this reading of XML? My Sample XML: <?xml version="1.0" encoding="UTF-8" standalone="no"?> <Objects> &#9;&#9;<Object type="Environment"> &#9;&#9;&#9;&#9;<name>environments\Animals.hwe</name> &#9;&#9;&#9;&#9;<type>Environment</type> &#9;&#9;</Object> &#9;&#9;<Object type="Environment"> &#9;&#9;&#9;&#9;<name>environments\ArtWorks.hwe</name> &#9;&#9;&#9;&#9;<type>Environment</type> &#9;&#9;</Object> &#9;&#9;<Object type="Environment"> &#9;&#9;&#9;&#9;<name>environments\Desktops.hwe</name> &#9;&#9;&#9;&#9;<type>Environment</type> &#9;&#9;</Object> &#9;&#9;<Object type="Environment"> &#9;&#9;&#9;&#9;<name>environments\DigitalSignage.hwe</name> &#9;&#9;&#9;&#9;<type>Environment</type> &#9;&#9;</Object> &#9;&#9;<Object type="Environment"> &#9;&#9;&#9;&#9;<name>environments\FlightControl.hwe</name> &#9;&#9;&#9;&#9;<type>Environment</type> &#9;&#9;</Object> &#9;&#9;<Object type="Environment"> &#9;&#9;&#9;&#9;<name>environments\startup.hwe</name> &#9;&#9;&#9;&#9;<type>Environment</type> &#9;&#9;</Object> </Objects> Br Michael
Posted
by Enevold.
Last updated
.
Post marked as solved
5 Replies
389 Views
Hi Forum I have several "function(s)" like this code below that open different environments.but I would like to separate the following line into 2 variables, as here below. let url = URL(string: "http...://192.168.0.196:8000/xmlcommand")! ServIP = 192.168.0.196 Port = 8000 So that the ServIP can be put into a "user" typed input Configuration field in my App so user can add different IP's for different systems this input field is asks users to enter the ServIP, and Port in this format "192.168.0.196:8000 so these can be reused in these functions, instead of hardcoded IP's. New string in the code should then look like this: let url = URL(string: "http...://ServIP:Port/xmlcommand")! I have been trying to split these up in some let lines but can't get it right. anyone can tell me how to split these into variables?         // Open Environment called "Environment_Name" func sendHttpPostAnimals() {        let session = URLSession(configuration: .default)        let url = URL(string: "http://192.168.0.196: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 Br Enevold
Posted
by Enevold.
Last updated
.
Post marked as solved
4 Replies
1.1k Views
Hi Forum I have been working on this code in Xcode PlayGrounds and it now works fint against my API, but I have quite some trouble creating a new project in Xcode and start make it paired with a button that initiate the http post. Anyone have an example on how I get this code working with a button? just one in the middel of the screen. Also how do I get this in a new Xcode project, if I creat a new I get errors. import SwiftUI // ********************************************** // // xs.services API code for iPhone/iPad&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;&#9;// // ********************************************** // 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 Thanks Michael
Posted
by Enevold.
Last updated
.
Post marked as solved
5 Replies
4.9k Views
Hi Forums I have been trying to work with this snippet but seems like I can't get it just right. I have added an explain here below as I can't post an image of what I'm trying to achieve. This should send the XML commands to my server API and initiate an Environment called "Animals.hwe" to be shown on a videowall. note: the xmlcommand URL is changed because of the http not being allowed. My test code... import UIKit import SwiftUI let session = URLSession(configuration: .default) let url = URL(string: "http........://192.168.0.196:8000/xmlcommand") var request = URLRequest(url: url) request.httpMethod = "POST" 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   // do something with the result } task.resume() // <- otherwise your network request won't be started With Rested App on Mac I can make the "POST" work fine with these settings: http. ..... ://192.168.0.196:8000/xmlcommand .................... POST Use Custom HTTP body - HTTPBody: <?xml version="1.0" encoding="UTF-8"?> <Commands> <command type="open"> <name>environments/Animals.hwe</name> <id>a1</id> </command> </Commands> Result from Rested application that works with the XML code... POST /xmlcommand Response time: 24 ms Request Headers & Body Accept: */* Accept-Encoding: gzip, deflate Content-Type: text/plain Accept-Language: en-us <?xml version="1.0" encoding="UTF-8"?> <Commands> <command type="open"> <name>environments/Animals.hwe</name> <id>a1</id> </command> </Commands> Response Headers HTTP/1.1 200 OK Date: Tue, 27 Oct 2020 18:03:53 GMT Cache-Control: no-cache Transfer-Encoding: Identity Not sure what goes wrong in my code but seems like the XML code does not reach the server API correctly. Any ideas would be greatly appreciated. Thanks Michael
Posted
by Enevold.
Last updated
.
Post marked as solved
4 Replies
624 Views
Hi ForumI'm trying to start making an ap that contains 3 - 6 buttons calledEnvironment1 to Environment6.I have been reading and reading on how to do this, with xcode/swift, but i think i'm stucked.I have a Controller API that listens to xml commands on its port http://ServIP:8000/xmlcommand/A typical http post command will look like this: e.g issued either on a command line or via postman.http://ServIP:8000/xmlcommand/XML Command: &lt;Commands&gt; &lt;command type=\"open\"&gt; &lt;name&gt;environments/Environment1.hwe&lt;/name&gt; &lt;id&gt;Environment1&lt;/id&gt; &lt;trans&gt;1.0&lt;/trans&gt; &lt;/command&gt; &lt;/Commands&gt;Anyone have an example they could share how to create a button that actually send this XMLcode to the API when pushed?Thank you in advance./Michael
Posted
by Enevold.
Last updated
.