Help: Swiftui http connection without button

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.

First, SwiftUI is not part of Swift, it's an Apple-private framework. This question is best asked over on Apple-specific developer forums, like the Apple developer site forums.

Second, How do you intend to activate this request? SwiftUI is designed along the lines of responding to UI events, including pressing a button. Is it your intention to execute this request one time only? Then you should probably put in your AppDelegate or SceneDelegate. Is it supposed to executed each time ContentView is refreshed? Then it should be placed outside the View stack. but within the body computed property. You'll need to explicitly return the ZStack { ... }. It seems like you need to re-think your design.

Hello Sir,

This is just a sort of test file that runs a part of a larger project. I was just testing it in a different file in order to make sure the part works properly.

The idea is to have a login which already works fine and if that login is successful the code mentioned here should be executed once automatically. It just sends a http request to a PHP-File run on a server and gets a return-message that consists of different filenames. Those should be displayed as single Images with the filenames underneath it.

I put the " print(dateien)" function instead of the following image showing because I can't print out an image there.

The only thing I would like to know is how I can get rid of the button but automatically send the request once.

How do I do that?

THANKS FOR THE ANSWER SO FAR.
Nikias

Sounds like it's an action that your login view should be taking as a consequence of a successful login. Go through the login, then execute this action as a post-action on a successful login result. No need for the button.

Terms of Service

Privacy Policy

Cookie Policy