iOS共享扩展中的API请求立即失败
您好,我正在使用共享扩展使用API请求(Alamofire)将一些数据发布到我的服务器,问题是请求立即失败,我不知道如何使其工作,我阅读了一些文章,我必须在后台使用URLSession发送请求,但我找不到任何示例来说明如何使其与Alamofire一起工作,以下是我在共享扩展ViewController中的代码:iOS共享扩展中的API请求立即失败,ios,swift,networking,alamofire,share-extension,Ios,Swift,Networking,Alamofire,Share Extension,您好,我正在使用共享扩展使用API请求(Alamofire)将一些数据发布到我的服务器,问题是请求立即失败,我不知道如何使其工作,我阅读了一些文章,我必须在后台使用URLSession发送请求,但我找不到任何示例来说明如何使其与Alamofire一起工作,以下是我在共享扩展ViewController中的代码: override func didSelectPost() { MessageHTTPHelper.submitMessage(contains: contentText, c
override func didSelectPost() {
MessageHTTPHelper.submitMessage(contains: contentText, completion: { (response) in
self.showAlert(title: "Result", message: response.result.isSuccess ? "SUCCESS" : "FAILURE")
})
}
MessageHTTPHelper.submitMessage
是我定义的一个助手函数,它在主应用程序中工作得非常好
我不关心响应,我只想发送请求而不需要任何回调,你能给我一个在iOS共享扩展中发送请求的例子吗?经过大量搜索和测试,失败了,终于为我工作了 这是我在
didSelectPost()中的代码
let body:参数=[
“版本”:配置S.currentReleaseVersion,
“内容”:cleanTextContent
]
HTTPHepler.makeHTTPRequest
只是一个助手方法,它使用给定参数创建Alamofire DataRequest实例并返回它
let request = HTTPHelper.makeHTTPRequest(route: "message",
headers: HTTPHelper.defaultAuthHTTPHeaders,
verb: .post,
body: body,
apiV1Included: true)
let queue = DispatchQueue(label: "com.example.background", qos: .background, attributes: .concurrent)
request.responseJSON(queue: queue, options: .allowFragments) { (response) in
if response.result.isFailure {
guard let message = response.error?.localizedDescription else {
self.dismiss()
return
}
self.showAlert(title: "Error", message: message)
}
}