下面的代码我试图从服务中获取详细信息。但由于HTTP加载失败,我收到错误 导入UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
parseData()
}
func parseData(){
let config = URLSessionConfiguration.default // Session Configuration
let session = URLSession(configuration: config) // Load configuration into Session
let url = URL(string: "Here is my service link"!
let task = session.dataTask(with: url, completionHandler:
{
(data, response, error) in
if error != nil {
print(error!.localizedDescription)
} else {
do {
if let json = try JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any] {
print(json)
}
} catch {
print("error in JSONSerialization")
}
}
})
task.resume()
}
}
以上是我试过的代码 - 请提出一些建议。
答案 0 :(得分:0)
将此添加到info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>