0

i have many apis that use the http request.I need them to work in the background in case the user put the app in the background and there are an api didn't finish the rquest process. I used this function but it doesn't work, it says : No exact matches in call to class method 'jsonObject'

    var request = URLRequest(url: URL(string:""!)
    
    request.httpMethod = "POST"
    
    request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
    request.addValue("application/json", forHTTPHeaderField: "Accept")
    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    
    let configuration = URLSessionConfiguration.background(withIdentifier: "app")
    configuration.allowsCellularAccess = true
    
    let session = URLSession(configuration: configuration, delegate: self, delegateQueue: nil)
   

    let task = session.downloadTask(with: request) { data, response, error in

        if error != nil {
            // Handle error…
            handler("Connection Error")
            return
        }
        let parsedResult: [String: AnyObject]!
        do {

            parsedResult = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: AnyObject]
            print(parsedResult)


        } catch {
            handler("Error loading data")
            return
        }

        if parsedResult["response_code"] as? Int == 24{
            handler("success")
        }else if parsedResult["response_code"] as? Int == 255{
            handler("unauthorized")
        }
        else{
            handler("\(parsedResult["response_code"] as? Int ?? 0)")
        }

        handler(nil)
    }
    
    task.resume()
basma
  • 1
  • 1
  • Please reference [this](https://developer.apple.com/documentation/foundation/jsonserialization) to understand what you can and can't use - what parameters to pass to the function – impression7vx Feb 26 '21 at 15:49
  • I only pass a token with string format as a bearer. – basma Feb 26 '21 at 17:07

0 Answers0