我有以下代码:
let urlPath:String = apiURL + apiVersion + url + "?api_key=" + apiKey let url = NSURL(string: urlPath) let session = NSURLSession.sharedSession() println(url!) let task = session.dataTaskWithURL(url!,completionHandler: {(data,reponse,error) in println("Task completed") // rest of the function... })
永远不会调用completionHandler函数.我尝试在浏览器中调用URL,它运行正常.我试过另一个URL,它仍然无法正常工作.我检查了我的ios模拟器可以连接到互联网,它确实.
任务永远不会完成,因为它永远不会开始.您必须使用resume()方法手动启动数据任务.
原文链接:https://www.f2er.com/swift/319882.htmllet urlPath = apiURL + apiVersion + url + "?api_key=" + apiKey let url = NSURL(string: urlPath)! let session = NSURLSession.sharedSession() let task = session.dataTaskWithURL(url) { data,response,error in print("Task completed") // rest of the function... } task.resume()