首先非常感谢代码的原作者,为了查询方便我就在这里又留了一份,请见谅.
原文http://www.hangge.com/blog/cache/detail_816.html
过去通过 NSURLConnection.sendSynchronousRequest() 方法能同步请求数据。从iOS9起,苹果建议废除 NSURLConnection,使用 NSURLSession 代替 NSURLConnection。
如果想要 NSURLSession 也能够同步请求,即数据获取后才继续执行下面的代码,使用信号、信号量就可以实现。
| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 
 | let urlString:String="http://www.hangge.com"
 let url:NSURL! = NSURL(string:urlString)
 
 let request:NSURLRequest = NSURLRequest(URL: url)
 
 let session = NSURLSession.sharedSession()
 
 let semaphore = dispatch_semaphore_create(0)
 
 let dataTask = session.dataTaskWithRequest(request,
 completionHandler: {(data, response, error) -> Void in
 if error != nil{
 print(error?.code)
 print(error?.description)
 }else{
 let str = NSString(data: data!, encoding: NSUTF8StringEncoding)
 print(str)
 }
 
 dispatch_semaphore_signal(semaphore)
 }) as NSURLSessionTask
 
 
 dataTask.resume()
 
 dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
 print("数据加载完毕!")
 
 
 |