API请求立即在iOS共享扩展中失败

时间:2018-09-02 06:59:05

标签: ios swift networking alamofire share-extension

您好,我正在使用共享扩展程序通过API请求(Alamofire)将一些数据发布到服务器,问题是请求立即失败,并且我不知道如何使它工作,我阅读了一些文章我必须使用URLSession在后台发送请求,但我找不到如何使其与alamofire一起使用的示例,这是我在共享扩展名ViewController中的代码:

override func didSelectPost() {

    MessageHTTPHelper.submitMessage(contains: contentText, completion: { (response) in
        self.showAlert(title: "Result", message: response.result.isSuccess ? "SUCCESS" : "FAILURE")
    })

}

MessageHTTPHelper.submitMessage是我定义的一个辅助函数,可以完美地在主应用程序中运行 我不在乎响应,我只想发送没有任何回调的请求,能否请给我一个在iOS共享扩展中发送请求的示例?

1 个答案:

答案 0 :(得分:0)

经过大量搜索和测试,但都失败了,this solution终于为我工作了!

这是我在public class OrderDetailsSite1 implements OrderDetailsComponent{ @FindBy(locator = "magento.ordered.item.name") private QAFWebElement productName; @FindBy(locator = "magento.ordered.item.quantity") private QAFWebElement productQuantity; @FindBy(locator = "magento.ordered.item.price") private QAFWebElement productUnitPrice; @FindBy(locator = "magento.ordered.item.subtotal") private QAFWebElement productSubtotal; } public class OrderDetailsSite2 implements OrderDetailsComponent { @FindBy(locator = "order.detail.product.name") private QAFWebElement productName; @FindBy(locator = "order.detail.product.uprice") private QAFWebElement productUnitPrice; @FindBy(locator = "order.detail.product.quantity") private QAFWebElement productQuantity; @FindBy(locator = "order.detail.product.subtotal") private QAFWebElement productSubtotal; } 中的代码

let body:参数= [         “版本”:Configs.currentReleaseVersion,         “内容”:cleanTextContent     ]

didSelectPost()

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) } } 只是一个帮助程序方法,它使用给定的参数创建Alamofire DataRequest实例并将其返回