NSURLSessionDownloadDelegate
class VIEwController: UIVIEwController,NSURLSessionDownloadDelegate { var session = NSURLSession() @IBOutlet weak var imagen: UIImageVIEw! @IBOutlet weak var progreso: UIProgressVIEw! @IBAction func cargar(sender: UIbutton) { let imageUrl: Nsstring = "http://c.hiphotos.baIDu.com/image/pic/item/8cb1cb13495409235fa14adf9158d109b2de4942.jpg" let getimageTask: NSURLSessionDownloadTask = session.downloadTaskWithURL(NSURL(string: imageUrl as String)!) getimageTask.resume() } overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let sessionConfig = NSURLSessionConfiguration.defaultSessionConfiguration() session = NSURLSession(configuration: sessionConfig,delegate: self,delegateQueue: nil) } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() } func URLSession(session: NSURLSession,downloadTask: NSURLSessionDownloadTask,dIDFinishDownloadingToURL location: NSURL){ print("Download finished") let downloadedImage = UIImage(data: NSData(contentsOfURL: location)!) dispatch_async(dispatch_get_main_queue(),{() in self.imagen.image = downloadedImage }) } func URLSession(session: NSURLSession,dIDWriteData bytesWritten: Int64,totalBytesWritten: Int64,totalBytesExpectedToWrite: Int64){ dispatch_async(dispatch_get_main_queue(),{() in let variable = float(totalBytesWritten)/float(totalBytesExpectedToWrite) self.progreso.progress = variable }) } }
NSURLSession
import UIKitclass VIEwController: UIVIEwController { @IBOutlet weak var city: UILabel! @IBOutlet weak var temperatureCelsius: UITextFIEld! @IBOutlet weak var temperatureCelsiusMax: UITextFIEld! @IBOutlet weak var temperatureCelsiusMin: UITextFIEld! @IBOutlet weak var temperatureKelvin: UITextFIEld! @IBOutlet weak var temperatureKelvinMax: UITextFIEld! @IBOutlet weak var temperatureKelvinMin: UITextFIEld! @IBOutlet weak var humIDity: UITextFIEld! @IBOutlet weak var wind: UITextFIEld! overrIDe func vIEwDIDLoad() { super.vIEwDIDLoad() let sessionConfig: NSURLSessionConfiguration = NSURLSessionConfiguration.defaultSessionConfiguration() sessionConfig.allowsCellularaccess = false //only accept JsON answer sessionConfig.httpAdditionalheaders = ["Accept":"application/Json"] //timeouts and connections allowed sessionConfig.timeoutIntervalForRequest = 30.0 sessionConfig.timeoutIntervalForResource = 60.0 sessionConfig.httpMaximumConnectionsPerHost = 1 //create session,assign configuration let session = NSURLSession(configuration: sessionConfig) session.dataTaskWithURL(NSURL(string: "http://API.openweathermap.org/data/2.5/weather?q=barcelona,es&appID=2de143494c0b295cca9337e1e96b00e0")!,completionHandler: {(data,response,error) in let dic:NSDictionary = (try? NSJsONSerialization.JsONObjectWithData(data!,options:NSJsONReadingOptions(rawValue: 0))) as? NSDictionary ?? [String:String]() if dic.count == 0 { return } print(dic) let city: Nsstring = (dic["name"] as! Nsstring) let kelvin: AnyObject! = (dic["main"] as! NSDictionary) ["temp"] let kelvin_min: AnyObject! = (dic["main"] as! NSDictionary) ["temp_min"] let kelvin_max: AnyObject! = (dic["main"] as! NSDictionary) ["temp_max"] let celsius = kelvin as! float - 274.15 as float let celsius_min = kelvin_min as! float - 274.15 as float let celsius_max = kelvin_max as! float - 274.15 as float let humIDity: AnyObject! = (dic ["main"] as! NSDictionary) ["humIDity"] let wind: AnyObject! = (dic ["wind"] as! NSDictionary) ["speed"] //original thread dispatch_async(dispatch_get_main_queue(),{ () in self.city.text = "\(city)" self.temperatureCelsius.text = "\(celsius)" self.temperatureCelsiusMax.text = "\(celsius_max)" self.temperatureCelsiusMin.text = "\(celsius_min)" self.temperatureKelvin.text = "\(kelvin)" self.temperatureKelvinMax.text = "\(kelvin_max)" self.temperatureKelvinMin.text = "\(kelvin_min)" self.humIDity.text = "\(humIDity)" self.wind.text = "\(wind)" }) }).resume() } overrIDe func dIDReceiveMemoryWarning() { super.dIDReceiveMemoryWarning() } }总结
以上是内存溢出为你收集整理的swift基础值网络请求全部内容,希望文章能够帮你解决swift基础值网络请求所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)