ios - swift中对api的Http后台请求
问题描述
我有许多使用 http 请求的 api。我需要它们在后台工作,以防用户将应用程序放在后台并且有一个 api 没有完成 rquest 过程。我使用了这个函数,但它不起作用,它说:没有完全匹配调用类方法'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()
解决方案
推荐阅读
- c# - RDLC 报告下载
- sql - pandas.read_sql 和 Oracle sql developer 输出不同的结果
- swift - 无法识别的选择器发送到实例 - UI 按钮 - Swift
- gitlab-ci - 从 gitlab ci 管道中检索宁静报告
- spring - Thymeleaf 形式 - 仅文本,无形式
- python - cv2.SIFT() 导致分段错误
- firebase - 将firebase crashlytics与flutter一起使用时出错
- r - R检查列表是否充满NULL的最快方法
- node.js - 如何从postgresql调用自定义函数使用反对js knex js
- python - 如何防止 pyinstaller 报错?