Posts

Post not yet marked as solved
1 Replies
1.4k Views
I need to post a https request to login view (SwiftUI), my code follow I have in getres.swift:so I neet to get value from response and put it in the text import Foundation import Combine struct result: Decodable { let res, ordercount, rate: String } class getres: ObservableObject { let objectWillChange = PassthroughSubject<getres, Never>() @Published var authenticated = "" @Published var todos = [result]() { didSet { objectWillChange.send(self) } } func auth(username: String, password: String) { guard let url = URL(string: "http://company.com/auth.php") else { return } let body: [String: String] = ["username": username, "password": password] let finalBody = try! JSONSerialization.data(withJSONObject: body) var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = finalBody request.setValue("application/json", forHTTPHeaderField: "Content-Type") URLSession.shared.dataTask(with: request) { (data, response, error) in guard let data=data else{return} let fineldata=try! JSONDecoder().decode(result.self, from: data) DispatchQueue.main.async { self.todos = [fineldata] self.authenticated=fineldata.res } print(fineldata) }.resume() } }and in the login page I try to show other viewin this code I will get values from function as json response I will get ordercount and rate I put it in the other view import SwiftUI struct ContentView: View { @State private var username: String = "" @State private var password: String = "" @ObservedObject var manager = getres() var body: some View { VStack(alignment: .leading) { if manager.authenticated == "2"{ userdetails() }else{ Text("Username") TextField("placeholder", text: $username) .textFieldStyle(RoundedBorderTextFieldStyle()) .border(Color.green) .autocapitalization(.none) Text("Password") SecureField("placeholder", text: $password) .textFieldStyle(RoundedBorderTextFieldStyle()) .border(Color.green) Button(action: { self.manager.auth(username: self.username, password: self.password) }) { HStack{ Spacer() Text("Login") Spacer() } .accentColor(Color.white) .padding(.vertical, 10) .background(Color.red) .cornerRadius(5) .padding(.horizontal, 40) } }.padding()} } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }and in userdetails this so I need to get values from response struct userdetails: View { @State var controller = getres() var body : some View{ ScrollView(Axis.Set.vertical, showsIndicators: true) { VStack(spacing: 20){ Image("wlogo").renderingMode(.original); HStack(spacing: 15){ Spacer() VStack(alignment: .center, spacing: 10) { Text(???).foregroundColor(Color.white).bold() .font(.largeTitle) Text("") .foregroundColor(Color.white) .font(.headline) } Spacer() }}}} how can I get ordercount from response and put intext(???)in the view userdetailsfor example controller.todos.ordercountI get this errorValue of type '[result]' has no member 'ordercount'when I tryText(controller.todos.ordercount)json response {"res":"2","ordercount":"20","rate":"5"}
Posted
by john250.
Last updated
.
Post not yet marked as solved
0 Replies
519 Views
I need to post a https request to login view (SwiftUI), my code follow I have in getres.swift:so I neet to get value from response and put it in the text import Foundation import Combine struct result: Decodable { let t=res, ordercount, rate: String } class getres: ObservableObject { let objectWillChange = PassthroughSubject<getres, Never>() @Published var authenticated = "" @Published var todos = [result]() { didSet { objectWillChange.send(self) } } func auth(username: String, password: String) { guard let url = URL(string: "http://company.com/auth.php") else { return } let body: [String: String] = ["username": username, "password": password] let finalBody = try! JSONSerialization.data(withJSONObject: body) var request = URLRequest(url: url) request.httpMethod = "POST" request.httpBody = finalBody request.setValue("application/json", forHTTPHeaderField: "Content-Type") URLSession.shared.dataTask(with: request) { (data, response, error) in guard let data = data else { return } let resData = try! JSONDecoder().decode(ServerMessage.self, from: data) print(resData.res) if resData.res == "2" { DispatchQueue.main.async { self.todos = [fineldata] self.authenticated=fineldata.res }} } }.resume() } }and in the login page I try to show other viewin this code I will get values from function as json response I will get ordercount and rate I put it in the other view import SwiftUI struct ContentView: View { @State private var username: String = "" @State private var password: String = "" @ObservedObject var manager = getres() var body: some View { VStack(alignment: .leading) { if manager.authenticated == "2"{ userdetails() }else{ Text("Username") TextField("placeholder", text: $username) .textFieldStyle(RoundedBorderTextFieldStyle()) .border(Color.green) .autocapitalization(.none) Text("Password") SecureField("placeholder", text: $password) .textFieldStyle(RoundedBorderTextFieldStyle()) .border(Color.green) Button(action: { self.manager.auth(username: self.username, password: self.password) }) { HStack{ Spacer() Text("Login") Spacer() } .accentColor(Color.white) .padding(.vertical, 10) .background(Color.red) .cornerRadius(5) .padding(.horizontal, 40) } }.padding()} } } struct ContentView_Previews: PreviewProvider { static var previews: some View { ContentView() } }and in userdetails this so I need to get values from response struct userdetails: View { @State var controller = getres() var body : some View{ ScrollView(Axis.Set.vertical, showsIndicators: true) { VStack(spacing: 20){ Image("wlogo").renderingMode(.original); HStack(spacing: 15){ Spacer() VStack(alignment: .center, spacing: 10) { Text(???).foregroundColor(Color.white).bold() .font(.largeTitle) Text("") .foregroundColor(Color.white) .font(.headline) } Spacer() }}}} how can I get ordercount from response and put intext(???)in the view userdetailsfor example controller.todos.ordercountI get this errorValue of type '[result]' has no member 'ordercount'when I tryText(controller.todos.ordercount)
Posted
by john250.
Last updated
.