HTTP Request in swift not working

后端 未结 6 1086
攒了一身酷
攒了一身酷 2021-02-03 10:19

I\'m trying to learn about working with APIs in Swift. As a good first test, I figured I\'d hit the itunes API and just return some search results. I\'m in playground using the

6条回答
  •  感动是毒
    2021-02-03 10:50

    Not sure why you aren't seeing an error—I get this:

    NSURLConnection/CFURLConnection HTTP load failed (kCFStreamErrorDomainSSL, -9807)

    You can test more effectively with httpbin.org—this works perfectly for me:

    let url = NSURL(string: "http://httpbin.org/get")
    
    let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
        println(NSString(data: data, encoding: NSUTF8StringEncoding))
    }
    
    task.resume()
    

提交回复
热议问题