从服务获取数据时HTTP加载失败

时间:2018-01-08 11:04:12

标签: ios json swift

下面的代码我试图从服务中获取详细信息。但由于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()
    }
}

以上是我试过的代码 - 请提出一些建议。

1 个答案:

答案 0 :(得分:0)

将此添加到info.plist

  <key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
</dict>