Swift : HttpRequest works on Simulator but not on Device (iPhone 11)

539 Views Asked by At

I have an Authentication Manager that send SignIn and SignUp request. Code works good on simulator and fetch data from database but not working on device. On device It gives http 500 error. I check the textfields before sending the request and they seem fine.

Any suggestions to solve this issue ?

func performSignIn(email: String, password: String){
    //1. Create URL
    if let url = URL(string: Constants.signInURL){
        
        var request = URLRequest(url: url)
        //Request Body
        var body = [String : Any]()
        body["user"] = ["email": email,
                        "password": password]
                
        do {
            request.httpBody  = try JSONSerialization.data(withJSONObject: body, options: [])
            
        } catch {
            print("JSON serialization failed:  \(error)")
            
        }
        // Change the URLRequest to a POST request
        request.httpMethod = "POST"
        //Need to tell that request has json in body.
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")

        //2. Create URLSession
        let session = URLSession(configuration: .default)
        
        //3. Give Session a task
        let task = session.dataTask(with: request) { (data, response, error) in
            if error != nil{
                DispatchQueue.main.async {
                    delegate?.failedWithError(error: error!)
                    return
                }
                
            }
            if let safeData = data{
                if let signInResponse = parseJSON(safeData){
                    //Who has the delegate run signInSuccess method
                    delegate?.signInSuccess(signInResponse)
                }
            }

        }
        //4. Start Task
        task.resume()
    }
}
0

There are 0 best solutions below