Post

Replies

Boosts

Views

Activity

Don't record JSONSerealization
Hi, I'm problem, when I add this line: request.addValue("application/json", forHTTPHeaderField: "Content-Type") To be able to see in the output, it doesn't write, but when I take this line of code, it writes. My code: URLSession.shared.dataTask(with: request) { data, response, error in                 if error == nil {                     guard let data = data else {return}                                          guard let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) else{                         return                     }                     print("Sucesso")                     print(json)                                          /*DispatchQueue.main.async(execute: {                         do {                         }catch {                                                      }                     })*/                                                               } else {                     print("Sem sucesso!")                 }             } .resume()
1
0
431
Sep ’22
Problem using JSONSerialization.jsonObject
Good afternoon guys, I have a project made in Xcode 9.2 and I have this problem, it doesn't record the information, I've already tested the API and ok, but I couldn't find the error yet: @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 {                                          // 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                                                          // 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!")                                 })                                                                                                 // 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!")                     })                 }                               // launch prepared session             } .resume()                      }              }
3
0
643
Sep ’22