NSURLSession详解
1 // MARK: - /* 使用NSURLSessionDataTask加载数据 */ 2 func sessionLoadData() 3 { 4 //创建NSURL对象 5 var url:NSURL! = NSURL(string: "http://m.weather.com.cn/data/101010100.html") 6 7 //创建请求对象 8 var request : NSURLRequest = NSURLRequest(URL: url) 9 10 let session = NSURLSession.sharedSession() 11 12 13 var dataTask = session.dataTaskWithRequest(request, completionHandler: { (var data:NSData?, var response:NSURLResponse?, var error:NSError?) -> Void in 14 15 //返回错误信息情况 16 if (error != nil) 17 { 18 println(error?.code) 19 println(error?.description) 20 }else 21 { 22 var jsonString = NSString(data: data!, encoding: NSUTF8StringEncoding) 23 println(jsonString) 24 } 25 }) as NSURLSessionTask 26 27 //使用resume方法启动任务 28 dataTask.resume() 29 } 30 31 32 33 // MARK: - /* 使用NSURLSessionDataTask下载文件 ---- 简单下载,不需要知道下载进度 */ 34 func sessionSimpleDownload() 35 { 36 //下载地址 37 var url = NSURL(string: "http://www.iphonetrain.com/shop/data/ios/201404/source/2809080296130100.jpg") 38 //请求 39 var request = NSURLRequest(URL: url!) 40 //连接 41 var session = NSURLSession.sharedSession() 42 //下载任务 43 var downloadTask = session.downloadTaskWithRequest(request, completionHandler: { (var location:NSURL?,var response:NSURLResponse?, var error:NSError?) -> Void in 44 45 // 输出下载文件原来的存放目录 46 println("location: \(location)") 47 48 //location位置转换 49 var locationPath = location?.path 50 51 //拷贝到我们自己目录中 52 let documents:String = NSHomeDirectory() + "/Documents/1.png" 53 54 // 创建文件管理器 55 var fileManager :NSFileManager = NSFileManager.defaultManager() 56 fileManager.moveItemAtPath(locationPath!, toPath: documents, error: nil) 57 println("location: \(documents)") 58 }) 59 60 //使用resume方法启动任务 61 downloadTask.resume() 62 } 63 64 65 // MARK: - /* 使用NSURLSessionDataTask下载文件 ---- 获取进度 */ 66 //创建一个下载模式--单利实现 67 func currentSession()->NSURLSession{ 68 69 var predicate:dispatch_once_t = 0 70 var currentSession:NSURLSession? = nil 71 72 dispatch_once(&predicate,{ 73 74 var config = NSURLSessionConfiguration.defaultSessionConfiguration() 75 76 currentSession = NSURLSession(configuration: config, delegate: self, delegateQueue: nil) 77 } 78 ) 79 return currentSession! 80 } 81 82 func sessionSeniorDownload() 83 { 84 //下载地址 85 var url = NSURL(string: "http://www.iphonetrain.com/shop/data/ios/201404/source/2809080296130100.jpg") 86 //请求 87 var request = NSURLRequest(URL: url!) 88 89 //连接 90 var session = currentSession() as NSURLSession 91 92 //下载任务 93 var downloadTask = session.downloadTaskWithRequest(request) 94 95 //使用resume方法启动任务 96 downloadTask.resume() 97 } 98 99 100 // MARK: - NSURLSessionDownloadDelegate 101 func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL) 102 { 103 //下载结束 104 println("下载结束") 105 106 } 107 func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) 108 { 109 //获取进度 110 var written:CGFloat = (CGFloat)(bytesWritten) 111 var total:CGFloat = (CGFloat)(totalBytesExpectedToWrite) 112 113 var pro:CGFloat = written / total 114 println("下按进度:\(pro)") 115 } 116 func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) 117 { 118 //下载偏移,主要用于暂停续传 119 } 120 121 122 123 124 // MARK: - /* 使用NSURLSessionDataTask上传文件 */ 125 func sessionUpload() 126 { 127 //上传的位置地址 128 var url = NSURL(string: "http://www.iphonetrain.com/shop/data/") 129 130 //请求 131 var request = NSURLRequest(URL: url!) 132 133 //连接 134 var session = NSURLSession.sharedSession() 135 136 //上传数据流 137 let documents:String = NSHomeDirectory() + "/Documents/1.png" 138 var imgData = NSData(contentsOfFile: documents) 139 140 var uploadTask = session.uploadTaskWithRequest(request, fromData: imgData, completionHandler: { (var data:NSData?, var response:NSURLResponse?, var error:NSError?) -> Void in 141 142 //上传完毕之后判断 143 println("上传完毕") 144 }) 145 146 //使用resume方法启动任务 147 uploadTask.resume() 148 }
时间: 2024-10-20 22:38:56