NSURLSession详解
// MARK: - /* 使用NSURLSessionDataTask加载数据 */ func sessionLoadData() { //创建NSURL对象 var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html") //创建请求对象 var request : NSURLRequest = NSURLRequest(URL: url) let session = NSURLSession.sharedSession() var dataTask = session.dataTaskWithRequest(request, completionHandler: { (var data:NSData?, var response:NSURLResponse?, var error:NSError?) -> Void in //返回错误信息情况 if (error != nil) { println(error?.code) println(error?.description) }else { var jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding) println(jsonString) } }) as NSURLSessionTask //使用resume方法启动任务 dataTask.resume() } // MARK: - /* 使用NSURLSessionDataTask下载文件 ---- 简单下载,不需要知道下载进度 */ func sessionSimpleDownload() { //下载地址 var url = NSURL(string: "http://www.iphonetrain.com/shop/data/ios/201404/source/2809080296130100.jpg") //请求 var request = NSURLRequest(URL: url!) //连接 var session = NSURLSession.sharedSession() //下载任务 var downloadTask = session.downloadTaskWithRequest(request, completionHandler: { (var location:NSURL?,var response:NSURLResponse?, var error:NSError?) -> Void in // 输出下载文件原来的存放目录 println("location: \(location)") //location位置转换 var locationPath = location?.path //拷贝到我们自己目录中 let documents:String = NSHomeDirectory() + "/Documents/1.png" // 创建文件管理器 var fileManager :NSFileManager = NSFileManager.defaultManager() fileManager.moveItemAtPath(locationPath!, toPath: documents, error: nil) println("location: \(documents)") }) //使用resume方法启动任务 downloadTask.resume() } // MARK: - /* 使用NSURLSessionDataTask下载文件 ---- 获取进度 */ //创建一个下载模式--单利实现 func currentSession()->NSURLSession{ var predicate:dispatch_once_t = var currentSession:NSURLSession? = nil dispatch_once(&predicate,{ var config = NSURLSessionConfiguration.defaultSessionConfiguration() currentSession = NSURLSession(configuration: config, delegate: self, delegateQueue: nil) } ) return currentSession! } func sessionSeniorDownload() { //下载地址 var url = NSURL(string: "http://www.iphonetrain.com/shop/data/ios/201404/source/2809080296130100.jpg") //请求 var request = NSURLRequest(URL: url!) //连接 var session = currentSession() as NSURLSession //下载任务 var downloadTask = session.downloadTaskWithRequest(request) //使用resume方法启动任务 downloadTask.resume() } // MARK: - NSURLSessionDownloadDelegate func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) { //下载结束 println("下载结束") } func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) { //获取进度 var written:CGFloat = (CGFloat)(bytesWritten) var total:CGFloat = (CGFloat)(totalBytesExpectedToWrite) var pro:CGFloat = written / total println("下按进度:\(pro)") } func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) { //下载偏移,主要用于暂停续传 } // MARK: - /* 使用NSURLSessionDataTask上传文件 */ func sessionUpload() { //上传的位置地址 var url = NSURL(string: "http://www.iphonetrain.com/shop/data/") //请求 var request = NSURLRequest(URL: url!) //连接 var session = NSURLSession.sharedSession() //上传数据流 let documents:String = NSHomeDirectory() + "/Documents/1.png" var imgData = NSData(contentsOfFile: documents) var uploadTask = session.uploadTaskWithRequest(request, fromData: imgData, completionHandler: { (var data:NSData?, var response:NSURLResponse?, var error:NSError?) -> Void in //上传完毕之后判断 println("上传完毕") }) //使用resume方法启动任务 uploadTask.resume() }