I am trying to segue to an entirely new view after I get a 200 OK response code from my API. The response code is successful sent back to the app and is printed into the console and the view is loaded. Except when the view loads in loads on top of the previous view. Photo of what I'm talking about. I can't use a navigation button because you can't have them preform functions and I don't want back button at the top left of the screen after a login. Here is the code I have now;
@State var username: String = ""
@State var password: String = ""
@State var sayHello = false
@State private var showingAreaView = false
@State private var showingAlert = false
@State private var alertTitle = ""
@State private var alertMessage = ""
Button(action: {
self.login()
})//end of login function
{
Image("StartNow 3").resizable()
.scaledToFit()
.padding()
}
if showingAreaView == true {
AreaView()
.animation(.easeIn)
}
//actual Login func
func login(){
let login = self.username
let passwordstring = self.password
guard let url = URL(string: "http://localhost:8000/account/auth/") else {return}
let headers = [
"Content-Type": "application/x-www-form-urlencoded",
"cache-control": "no-cache",
"Postman-Token": "89a81b3d-d5f3-4f82-8b7f-47edc39bb201"
]
let postData = NSMutableData(data: "username=\(login)".data(using: String.Encoding.utf8)!)
postData.append("&password=\(passwordstring)".data(using: String.Encoding.utf8)!)
let request = NSMutableURLRequest(url: NSURL(string: "http://localhost:8000/account/auth/")! as URL,
cachePolicy:.useProtocolCachePolicy, timeoutInterval: 10.0)
request.httpMethod = "POST"
request.allHTTPHeaderFields = headers
request.httpBody = postData as Data
let session = URLSession.shared
let dataTask = session.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
if let httpResponse = response as? HTTPURLResponse {
guard let data = data else {return}
print(data)
if httpResponse.statusCode == 200{
DispatchQueue.main.async {
//Segue to new view goes here
print(httpResponse.statusCode)
self.showingAreaView = true
}
}else{
if httpResponse.statusCode == 400{
DispatchQueue.main.async {
self.alertTitle = "Oops"
self.alertMessage = "Username or Password Incorrect"
self.showingAlert = true
print(httpResponse.statusCode)
}
}else{
DispatchQueue.main.async {
self.alertTitle = "Well Damn"
self.alertMessage = "Ay chief we have no idea what just happened but it didn't work"
self.showingAlert = true
}
}
}
do{
let JSONFromServer = try JSONSerialization.jsonObject(with: data, options: [])
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
let tokenArray = try decoder.decode(token.self, from: data)
print(tokenArray.token)
UserDefaults.standard.set(tokenArray.token, forKey: "savedToken")
let savedToken = UserDefaults.standard.object(forKey: "savedToken")
print(savedToken)
}catch{
if httpResponse.statusCode == 400{
self.alertTitle = "Oops"
self.alertMessage = "Username or Password Incorrect"
self.showingAlert = true
}
print(error)
print(httpResponse.statusCode)
}
} else if let error = error {
self.alertTitle = "Well Damn"
self.alertMessage = "Ay chief we have no idea what just happened but it didn't work"
self.showingAlert = true
print(error)
}
})
dataTask.resume()
}
I've tired to google this issue but haven't had any luck. All I need is a basic segue to a new view without the need of a NavigationButton.
Any help is appreciated!