I have an alert in Swift that is supposed to close when the “OK” button is clicked. Unfortunately, this doesn’t happen. Visually, I can see in the app that the “OK” button is clickable once, but nothing happens afterward. Additionally, the “OK” button becomes unclickable, and the debug message “OK clicked” is never displayed.
import SwiftUI
struct MobileDeviceDetailView: View {
@ObservedObject var api: JamfAPI
var deviceId: String
var deviceName: String
@State private var showAlert = false
@State private var showConfirmationDialog = false
@State private var alertMessage = ""
@State private var alertTitle = "Info"
var body: some View {
NavigationStack {
Form {
Section(
header: Text("Action"),
content: {
Button(action: {
// Update Inventory
api.sendCommandDevice(
deviceId: deviceId,
command: "UpdateInventory"
) { success, message in
self.alertMessage = message
self.alertTitle = success ? "Done" : "Error"
self.showAlert = true
}
}) {
Text("Update Inventory")
.padding()
.cornerRadius(10)
}
.frame(maxWidth: .infinity)
.padding(.horizontal)
.overlay(
RoundedRectangle(cornerRadius: 10)
.stroke(Color.blue, lineWidth: 2)
.padding(5)
)
})
}
}
.alert(isPresented: $showAlert) {
Alert(
title: Text(alertTitle),
message: Text(alertMessage),
dismissButton: .default(Text("OK")) {
print("OK clicked")
self.showAlert = false // Zustand zurücksetzen
}
)
}
}
}
Here the important API part
if let httpResponse = response as? HTTPURLResponse {
print(httpResponse.statusCode)
if httpResponse.statusCode == 201 {
DispatchQueue.main.async {
completion(true, "(command) wurde durchgeführt")
}
} else {
DispatchQueue.main.async {
completion(false, "Fehler, versuch es später erneut")
}
}
}
I see in the debug logs the status 201.
4