Reputation: 5101
I am working on my first iOS app.
There is a viewController that checks if the user has already signed in.
print("Usuario actual logeado:",logeado_actual ?? "ninguno")
if (logeado_actual == "OK"){
print("logeado:")
self.performSegue(withIdentifier: "sinlogear", sender: nil)
}
The print output is
Usuario actual logeado: OK
logeado:
that means that the segue should be executed, but it doesn't.
The segue exists and the Identifier is sinlogear
Is there anything missing or not valid in my code?
EDIT
Full code:
import Alamofire
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var txtEmail: UITextField!
@IBOutlet weak var txtPassword: UITextField!
@IBOutlet weak var btnAcceder: UIButton!
@IBOutlet weak var btnContinuar: UIButton!
@IBOutlet weak var btnRegistro: UIButton!
@IBOutlet weak var btnOlvidar: UIButton!
//Defined a constant that holds the URL for our web service
let URL_USER_REGISTER = "https://.../android_api/login.php"
struct Usuario: Codable {
var id: Int
var activacion: Int
var autorizado: Int
var cif: String
var correo: String
var cuenta: String
var direccion: String
var empresa: String
var facebook_id: String
var foto_usuario: String
var instagram_id: String
var num_estrellas: Int
var tel: String
var token_firebase: String
var twitter_id: String
var usuario: String
var nombre: String
}
@IBOutlet weak var txtLabel: UILabel!
@IBOutlet weak var labelMessage: UILabel!
let defaults = UserDefaults.standard
@IBAction func btnLogin(_ sender: Any) {
print("login pulsado")
//creating parameters for the post request
let parameters: Parameters=[
"email":txtEmail.text!,
"password":txtPassword.text!
]
print(txtEmail.text!)
self.txtLabel.text = ""
//Sending http post request
Alamofire.request(URL_USER_REGISTER, method: .post, parameters: parameters).responseJSON
{
response in
//printing response
print(response)
//getting the json value from the server
if let resultado = response.result.value {
//converting it as NSDictionary
let jsonData = resultado as! NSDictionary
let id = jsonData.value(forKey:"id") as! NSNumber?
let estado = jsonData.value(forKey: "error") as! NSNumber?
if estado == 0 {
print("login:","OK")
self.txtLabel.text = "Datos correctos"
print("error:",estado!)
print("id user:",id!)
if let json = response.result.value as? [String: Any] {
//usuario
do {
guard let user = json["user"] as? [String: Any],
let usuario = user["usuario"] as? String
else {
print("Failed to parse JSON")
return
}
self.defaults.set(usuario, forKey: "usuario")
self.defaults.set("OK", forKey: "logeado")
print(usuario)
}
//nombre
do {
guard let user = json["user"] as? [String: Any],
let nombre = user["nombre"] as? String
else {
print("Failed to parse JSON")
return
}
self.defaults.set(nombre, forKey: "nombre")
print(nombre)
}
//imageb
do {
guard let user = json["user"] as? [String: Any],
let foto_usuario = user["foto_usuario"] as? String
else {
print("Failed to parse JSON")
return
}
self.defaults.set(foto_usuario, forKey: "foto_usuario")
print(foto_usuario)
}
//correo
do {
guard let user = json["user"] as? [String: Any],
let correo_usuario = user["correo"] as? String
else {
print("Failed to parse JSON")
return
}
self.defaults.set(correo_usuario, forKey: "correo")
print(correo_usuario)
}
}
}
else {
print("login:","NO OK")
self.txtLabel.text = "Datos incorrectos"
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
let usuario_actual = self.defaults.string(forKey: "usuario")
let nombre_usuario_actual = self.defaults.string(forKey: "nombre")
let foto_usuario_actual = self.defaults.string(forKey: "foto_usuario")
let correo_usuario_actual = self.defaults.string(forKey: "correo")
let logeado_actual = self.defaults.string(forKey: "logeado")
print("Usuario actual:",usuario_actual ?? "ninguno")
print("Usuario actual nombre:",nombre_usuario_actual ?? "ninguno")
print("Usuario actual foto:",foto_usuario_actual ?? "ninguno")
print("Usuario actual correo:",correo_usuario_actual ?? "ninguno")
print("Usuario actual logeado:",logeado_actual ?? "ninguno")
performSegue(withIdentifier: "misegue", sender: self)
if (logeado_actual == "OK"){
print("logeado:")
}
//redondear botones
btnAcceder.layer.cornerRadius = 5
btnAcceder.layer.borderWidth = 1
btnAcceder.layer.borderColor = UIColor.white.cgColor
btnOlvidar.layer.cornerRadius = 5
btnOlvidar.layer.borderWidth = 1
btnOlvidar.layer.borderColor = UIColor.white.cgColor
btnRegistro.layer.cornerRadius = 5
btnRegistro.layer.borderWidth = 1
btnRegistro.layer.borderColor = UIColor.white.cgColor
btnContinuar.layer.cornerRadius = 5
btnContinuar.layer.borderWidth = 1
btnContinuar.layer.borderColor = UIColor.white.cgColor
}
}
Upvotes: 1
Views: 39
Reputation: 536
You should change the performSegue a bit. But first.
1.) Remove the current segue (by clicking the connection in the storyboard and delete it)
2.) Drag a new segue (like you did when created your current one)
3.) Add the same identifier
4.) Fix your performSegue like:
performSegue(withIdentifier: "sinlogear", sender: self)
————
EDIT: A segue cannot be performed from viewDidLoad()
because it’s too early. This function is runned before the storyboard is there. Consider using viewDidAppear
override func viewDidAppear(){
//perform the segue here
}
Upvotes: 3