Hello guys,
I´m new to the forum and have a question concerning a http request which should be established without a button clicked. Basically I would like to have everything inside the button´s action tag done automatically without a button clicked.
Thanks in advance.
Nikias
Here is my code from the ts.view :
import SwiftUI
struct ContentView: View {
@State var username: String = "..."
@State var password: String = "..."
var body: some View {
ZStack {
VStack {
Button(action: {
if 1==1 {
let myUrl = URL(string: "
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)")
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 {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
// Now we can access value of First Name by its key
let firstNameValue = parseJSON["Name"] as? String
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue?.components(separatedBy: ",")
print(dateien)
}
} catch {
print(error)
}
}
task.resume()
}
}) {
LoginButtonContent()
}
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I had to remove the URL.
I´m new to the forum and have a question concerning a http request which should be established without a button clicked. Basically I would like to have everything inside the button´s action tag done automatically without a button clicked.
Thanks in advance.
Nikias
Here is my code from the ts.view :
import SwiftUI
struct ContentView: View {
@State var username: String = "..."
@State var password: String = "..."
var body: some View {
ZStack {
VStack {
Button(action: {
if 1==1 {
let myUrl = URL(string: "
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
let postString = "Name=\($username)&Passwort=\($password)";
request.httpBody = postString.data(using: String.Encoding.utf8);
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)")
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 {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
// Now we can access value of First Name by its key
let firstNameValue = parseJSON["Name"] as? String
print("firstNameValue: \(firstNameValue)")
let dateien = firstNameValue?.components(separatedBy: ",")
print(dateien)
}
} catch {
print(error)
}
}
task.resume()
}
}) {
LoginButtonContent()
}
}
}
}
}
struct TestView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I had to remove the URL.