Hello,
the function an the request work perfectly fine but now I´like to view the array "Dateien" that I get off the HTTP-Request with a For- loop: There should be an Image an the filename for each string in the array "Dateien", which works fine with the example array.
import SwiftUI
struct ContentView4: View {
@State var showingDetail = false
@State var username: String = "."
@State var password: String = "."
let dateien = Optional(["word.png", "vi.png", "text.png", "pp.png", "pdf.png", "ordner.png", "ex.png", "datei.png", "bild.png"])
var body: some View {
ScrollView(.vertical) {
ZStack{
VStack {
ForEach(0 ..< dateien!.count) {
Button(action: {
print("button pressed")
self.showingDetail.toggle()
}) {
Image("datei")
}
.scaledToFit()
.padding(0)
Text(self.dateien![$0])
}
}
}
.sheet(isPresented: $showingDetail) {
ts()
}
.onAppear { //This `onAppear` is added to `ZStack{...}`
doHttpRequest()
}
}
}
But I don't get how to use the Array" Dateien" from the Http-Request instead of the example one.
Heeres my code for the request:
func doHttpRequest() {
let myUrl = URL(string: "http://192.168.1.180/int.php")! //Trailing semicolon is not needed
var request = URLRequest(url: myUrl)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)"
request.httpBody = postString.data(using: .utf8)
let task = URLSession.shared.dataTask(with: request) {
(data, response, error) in
//Use if-let when you want to use the unwrapped value
if let error = error {
print("error=\(error)")
return
}
//Use guard-let when nil has no meaning and want to exit on nil
guard let response = response else {
print("Unexpected nil response")
return
}
// You can print out response object
print("response = \(response)")
//Let's convert response sent from a server side script to a NSDictionary object:
do {
//Use guard-let when nil has no meaning and want to exit on nil
guard let data = data else {
print("Unexpected nil data")
return
}
//#1 `mutableContainer` has no meaning in Swift
//#2 Use Swift Dictionary type instead of `NSDictionary`
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
if let parseJSON = json {
// Now we can access value of First Name by its key
//Use if-let when you want to use the unwrapped value
if let firstNameValue = parseJSON["Name"] as? String {
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue.components(separatedBy: ",")
print(dateien)
let greeting = dateien
}
}
} catch {
print(error)
}
}
task.resume()
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView4()
}
}
struct ts: View {
var body: some View {
NavigationView {
VStack {
Text("Datei.pdf")
.font(.system(size: 60))
.foregroundColor(.black)
.padding(50)
Text("+")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.yellow)
.cornerRadius(35.0)
Text("Datei herunterladen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.blue)
.cornerRadius(35.0)
Text("Datei umbenennen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.green)
.cornerRadius(35.0)
Text("Datei löschen")
.font(.headline)
.foregroundColor(.white)
.padding()
.frame(width: 220, height: 60)
.background(Color.red)
.cornerRadius(35.0)
}
}
}
}
I would be very glad I you help me out for another time.
Thanks
Nikias