gpt4 book ai didi

swift - URLSession 崩溃应用程序且没有日志

转载 作者:行者123 更新时间:2023-11-30 11:50:03 27 4
gpt4 key购买 nike

我正在尝试使用 Swift 向带有一些 JSON 数据的 php 脚本发出 POST 请求。当我运行以下代码时,我的应用程序崩溃并且没有信息记录到控制台。

if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
let url = NSURL(string: "https://www.centrohq.com/api/payment/")!
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData

let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
print(response)
})
task.resume()
}

最佳答案

我会尝试解开网址。我过去遇到过一些问题,因为我没有安全地解开传入的网址:

if let jsonData = try? JSONSerialization.data(withJSONObject: dict, options: .prettyPrinted) {
if let url = NSURL(string: "https://www.centrohq.com/api/payment/") {
let request = NSMutableURLRequest(url: url as URL)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData

let task = URLSession.shared.dataTask(with: request as URLRequest, completionHandler: { (data, response, error) in
print(response)
})
task.resume()
}
}

关于swift - URLSession 崩溃应用程序且没有日志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48430151/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com