elkin sepulveda elkin sepulveda - 4 months ago 10
iOS Question

Register and Log in swift

Hi i am trying a simple app with registration and when is complete , go to home view , but when i try to go HomeView my app crashes

this is the code

@IBAction func Registrar(sender: AnyObject) {

let userName = UserName.text!
let userEmail = UserEmail.text!
let medicRegistry = MedicRegistry.text!
let specialty = Speciality.text!
let country = Country.text!
let city = Ciudad.text!
let userPass = UserPassword.text!
let repeatPass = RepeatPassword.text!



if ( userName.isEmpty || userEmail.isEmpty || medicRegistry.isEmpty || speciality.isEmpty || country.isEmpty || city.isEmpty || userPass.isEmpty || userEmail.isEmpty ) {

let alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = "Por favor llene todos los campos"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()
} else if ( userPass != repeatPass) {

let alertView:UIAlertView = UIAlertView()
alertView.title = "Sign Up Failed!"
alertView.message = "Passwords doesn't Match"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()

//NSUserDefaults.standarUserDefaults().setObject(id, forKey:"userId")
//NSUserDefaults.standarUserDefaults().synchronize()
}else if check == false{
let alertView:UIAlertView = UIAlertView()
alertView.title = "Fallo al Registrar!"
alertView.message = "Debes aceptar los terminos y condiciones"
alertView.delegate = self
alertView.addButtonWithTitle("OK")
alertView.show()


}

else {

let url_servicio = "http://mydirection.php"

let request = NSMutableURLRequest(URL: NSURL(string: url_servicio)!)
request.HTTPMethod = "POST"
let postString = "nombre=\(userName)&email=\(userEmail)&registro=\(medicRegistry)&esp=\(specialty)&pais=\(country)&ciudad=\(city)&contrasena=\(userPass)"
request.HTTPBody = postString.dataUsingEncoding(NSUTF8StringEncoding)
let task = NSURLSession.sharedSession().dataTaskWithRequest(request) { data, response, error -> Void in
if (error != nil){
print(error!.localizedDescription)
}else{
let nsdata:NSData = NSData(data: data!)

do{
let json = try NSJSONSerialization.JSONObjectWithData(nsdata, options: NSJSONReadingOptions.MutableContainers)
let Id = json["id"] as! String
let c: String = ""


if (Id != c){
print (Id)
self.performSegueWithIdentifier("funciona", sender: self)
}
}
catch{
print("Error del JSON")

}
}
}
task.resume()

}
}
}


Error
i manage to print the Id but when then just crash
i am new in this and i dont know whats wrong
thx for ur help.

enter image description here

Answer

The answer is in the error message: "Only run on the main thread".

Your completion handler runs in the background but is trying to change the UI using a segue. You should dispatch your performSegueWithIdentifier back to the main thread.