Hi guys.
Help me!
My API is perfect.
My code Swift is stranger error, but it writes perfectly to the database via API.
Code:
@IBAction func register_click(_ sender: Any) {
//if not text
if usernameTxt.text!.isEmpty || passwordTxt.text!.isEmpty || emailTxt.text!.isEmpty || firstnameTxt.text!.isEmpty || lastnameTxt.text!.isEmpty {
usernameTxt.attributedPlaceholder = NSAttributedString(string: "username", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
passwordTxt.attributedPlaceholder = NSAttributedString(string: "password", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
emailTxt.attributedPlaceholder = NSAttributedString(string: "email", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
firstnameTxt.attributedPlaceholder = NSAttributedString(string: "name", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
lastnameTxt.attributedPlaceholder = NSAttributedString(string: "surname", attributes: [NSAttributedStringKey.foregroundColor: UIColor.red])
} else {
// if entered - create new user mysql
// remove keyboard
self.view.endEditing(true)
// url to php file
let url = URL(string: "http://localhost/Twiter/register.php")
// request to php file
var request = URLRequest(url: url!)
// method to pass data to this file (e.g. via POST)
request.httpMethod = "POST"
//request.addValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
//request.addValue("application/json", forHTTPHeaderField: "Accept")
//request.setValue( "application/json", forHTTPHeaderField: "Authorization")
//request.setValue("application/json; application/x-www-form-urlencoded", forHTTPHeaderField: "Content-Type")
// body to be appended to url
let body = "username=\(usernameTxt.text!.lowercased())&password=\(passwordTxt.text!)&email=\(emailTxt.text!)&fullname=\(firstnameTxt.text!)%20\(lastnameTxt.text!)"
request.httpBody = body.data(using: .utf8) //allowLossyConversion: true
// proceed request URLSession.shared.dataTask(with: request) { data, response, error in
URLSession.shared.dataTask(with: request) { data, response, error in
if error == nil {
//=================================================================================
guard let data = data else {return}
// get main queue in code process to communicate back to UI
DispatchQueue.main.async(execute: {
do {
// get json result
let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? NSDictionary //.allowFragments
// assign json to new var parseJSON in guard/secured way
guard let parseJSON = json else {
print("Error parsing parsing")
return
}
// get id from parseJSON dictionary
let id = parseJSON["id"]
// successfully registered
if id != nil {
//print(parseJSON)
/*DispatchQueue.main.async(execute: {
let message = parseJSON["message"] as! String
appDelegate.infoView(message: message, color: colorLightGreen)
print("Sucesso!")
})*/
// save user information we received from our host
UserDefaults.standard.set(parseJSON, forKey: "parseJSON")
user = UserDefaults.standard.value(forKey: "parseJSON") as? NSDictionary
// go to tabbar / home page
DispatchQueue.main.async(execute: {
//appDelegate.login()
print("Sucesso")
})
// error
} else {
//get main queue to comunicate back to user
DispatchQueue.main.async(execute: {
let message = parseJSON["message"] as! String
//appDelegate.errorView(message: message)
appDelegate.infoView(message: message, color: colorSmoothRed)
print("Erro sem sucesso\(parseJSON)")
})
}
}
catch {
DispatchQueue.main.async(execute: {
let message = "Erro de Comunicação\(error)"
appDelegate.infoView(message: message, color: colorSmoothRed)
})
}
})
// if unable to proceed request
} else {
// get main queue to comunicate back to user
DispatchQueue.main.async(execute: {
let message = error!.localizedDescription
appDelegate.infoView(message: message, color: colorSmoothRed)
print("sem comunicação!")
})
//=====================================================================================
// } else {
// print("Sem sucesso!")
}
// launch prepared session
} .resume()
}
The message is: "Erro ComunicaçãoError
Domain = NSCocoaErrorDomain code=3840" JSON ....