I wanted to show a progress view during a simple URL POST request process. What I would like to do is the button to turn into a ProgressView spinner (usually it's default) as it's going through the requestTest function process. After the request is done, then the progress view goes away and turns back into a button.
here's the code.
struct ContentView: View {
@State private var tweetID = ""
@State private var tweetStatus = ""
@State private var response = ""
@State var showAlert = false
@State var sendToWebhook = false
var body: some View {
NavigationView {
Form {
Section(footer: Text("Test")) {
TextField("Field to place response data", text: $response)
TextEditor( text: $tweetStatus)
.frame(height: 100)
}
Section {
Button("Get Data") {
// Where progress should start before function
ProgressView("Test", value: 100, total: 100)
requestTest() { results in
response = results
if response == "No Data!" {
showAlert = true
}
}
}
if self.requestTest {
ProgressView()
}
}
}
.alert(isPresented: $showAlert) {
Alert(title: Text("Tweet Sent"), message: Text("Your Tweet is sent! Your Tweet ID is shown in the field"), dismissButton: .default(Text("OK")))
}
}
}
func requestTest(completion: @escaping(String) -> ()) {
if let url = URL(string: "https://requestbin.net/r/ag4ipg7n") {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.queryItems = [ URLQueryItem(name: "TweetID", value: response),
URLQueryItem(name: "Status", value: tweetStatus)]
if let query = components.url!.query {
request.httpBody = Data(query.utf8)
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data,
let apiResponse = String(data: data, encoding: .utf8) {
// IF Completed, these actions are shown below
completion(apiResponse)
self.showAlert = true
tweetStatus = ""
} else {
completion("No Data!")
}
}
task.resume()
}
}
}
I thought if I tried to do as a if self.requestTest { ProgressView() }, but no avail as it throwed me a error that says Cannot convert value of type '(@escaping (String) -> ()) -> ()' to expected condition type 'Bool'.
Is there a way to do that?
Post
Replies
Boosts
Views
Activity
I have a URL session to send to a webhook. I have already set up the query which are strings, but one I tried to do along with a Stepper control value, and added it to the list of URLQueryItem, and I got a error that says Cannot convert value of type 'int' to expected argument type 'string'. So it turns out the value in the Stepper is actually a interger (or a Int type) and not a String.
here's the code
struct ContentView: View {
@State private var TweetStatus = ""
@State private var response = ""
@State var showAlert = false
@State var SendToWebhook = false
@State var StepperValue = 0
var body: some View {
NavigationView {
Form {
Section(footer: Text("Test")) {
TextField("Field to place response data", text: $response)
TextEditor( text: $TweetStatus)
.frame(height: 100)
// Stepper
Stepper(value: $StepperValue, in: 0...10080) {
Text("Duration in Minutes: \(StepperValue)")
}
}
Section {
Button("Get Data") {
requestTest() { results in
response = results
if response == "No Data!" {
showAlert = true
}
}
}
}
}
.alert(isPresented: $showAlert) {
Alert(title: Text("Tweet Sent"), message: Text("Your Tweet is sent! Your Tweet ID is shown in the field"), dismissButton: .default(Text("OK")))
}
}
}
func requestTest(completion: @escaping(String) -> ()) { // <--- here completion closure
if let url = URL(string: "http://YIKES") {
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.setValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
var components = URLComponents(url: url, resolvingAgainstBaseURL: false)!
components.queryItems = [ URLQueryItem(name: "TweetID", value: response),
URLQueryItem(name: "Status", value: TweetStatus),
URLQueryItem(name: "PollDuration", value: StepperValue)]
if let query = components.url!.query {
request.httpBody = Data(query.utf8)
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let data = data,
let apiResponse = String(data: data, encoding: .utf8) {
// IF Completed, these actions are shown below
completion(apiResponse) // <--- here
self.showAlert = true
TweetStatus = ""
} else {
completion("No Data!") // <--- here
}
}
task.resume()
}
}
}
So what I am trying to do is to pass the id StepperValue as a string to get it into the URLQueryItem. I can't even use a String(SOMETHING), to no avail either. I think it's Swift 5.5 so there have to be some code changes to this.
I made a simple SwiftUI webhook app in Swift Playgrounds that surprisingly supports URLSession when making a HTTP request. Now what I would like to see if there is a way to take that response (for instance, from RequestBIN) after a POST request, and prefill it into a simple text area.
Here’s the code
struct ContentView: View {
@State private var TweetID = ""
var body: some View {
NavigationView {
Form {
Section(footer: Text("Where it should place the response data")) {
TextField("Field to place response data", text: $TweetID)
}
Section {
Button("Get Data") {
self.RequestTest()
}
}
}
}
}
func RequestTest() {
let semaphore = DispatchSemaphore (value: 0)
let parameters = "Foo=Bar"
let postData = parameters.data(using: .utf8)
var request = URLRequest(url: URL(string: "https://YIKES")!,timeoutInterval: Double.infinity)
request.addValue("application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
request.httpMethod = "POST"
request.httpBody = postData
let task = URLSession.shared.dataTask(with: request) { data, response, error in
guard let data = data else {
print(String(describing: error))
semaphore.signal()
return
}
print(String(data: data, encoding: .utf8)!)
semaphore.signal()
}
task.resume()
semaphore.wait()
}
}
note that I guess the code print(String(data: data, encoding: .utf8)!) that whereas data is the response needed to fill in the text field which is......I guess the ID or varible scope whatmacallsit $TweetID