标签:
1 func sessionLoadData(){ 2 //创建NSURL对象 3 let urlString:String="http://hangge.com" 4 var url:NSURL! = NSURL(string:urlString) 5 //创建请求对象 6 var request:NSURLRequest = NSURLRequest(URL: url) 7 8 let session = NSURLSession.sharedSession() 9 10 var dataTask = session.dataTaskWithRequest(request, 11 completionHandler: {(data:NSData!, response:NSURLResponse!, error:NSError!) -> Void in 12 if error != nil{ 13 println(error?.code) 14 println(error?.description) 15 }else{ 16 var str = NSString(data: data!, encoding: NSUTF8StringEncoding) 17 println(str) 18 } 19 }) as NSURLSessionTask 20 21 //使用resume方法启动任务 22 dataTask.resume() 23 }
1 func sessionSimpleDownload(){ 2 //下载地址 3 var url = NSURL(string: "http://hangge.com/blog/images/logo.png") 4 //请求 5 var request:NSURLRequest = NSURLRequest(URL: url!) 6 7 let session = NSURLSession.sharedSession() 8 9 //下载任务 10 var downloadTask = session.downloadTaskWithRequest(request, 11 completionHandler: { (location:NSURL!, response:NSURLResponse!, error:NSError!) -> Void in 12 //输出下载文件原来的存放目录 13 println("location:\(location)") 14 //location位置转换 15 var locationPath = location.path 16 //拷贝到用户目录 17 let documnets:String = NSHomeDirectory() + "/Documents/1.png" 18 //创建文件管理器 19 var fileManager:NSFileManager = NSFileManager.defaultManager() 20 fileManager.moveItemAtPath(locationPath!, toPath: documnets, error: nil) 21 println("new location:\(documnets)") 22 }) 23 24 //使用resume方法启动任务 25 downloadTask.resume() 26 }
1 import UIKit 2 3 class ViewController: UIViewController ,NSURLSessionDownloadDelegate { 4 5 override func viewDidLoad() { 6 super.viewDidLoad() 7 8 sessionSeniorDownload() 9 } 10 11 //下载文件 12 func sessionSeniorDownload(){ 13 //下载地址 14 var url = NSURL(string: "http://hangge.com/blog/images/logo.png") 15 //请求 16 var request:NSURLRequest = NSURLRequest(URL: url!) 17 18 let session = currentSession() as NSURLSession 19 20 //下载任务 21 var downloadTask = session.downloadTaskWithRequest(request) 22 23 //使用resume方法启动任务 24 downloadTask.resume() 25 } 26 27 //创建一个下载模式 28 func currentSession() -> NSURLSession{ 29 var predicate:dispatch_once_t = 0 30 var currentSession:NSURLSession? = nil 31 32 dispatch_once(&predicate, { 33 var config = NSURLSessionConfiguration.defaultSessionConfiguration() 34 currentSession = NSURLSession(configuration: config, delegate: self, delegateQueue: nil) 35 }) 36 return currentSession! 37 } 38 39 //下载代理方法,下载结束 40 func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, 41 didFinishDownloadingToURL location: NSURL) { 42 //下载结束 43 println("下载结束") 44 45 //输出下载文件原来的存放目录 46 println("location:\(location)") 47 //location位置转换 48 var locationPath = location.path 49 //拷贝到用户目录 50 let documnets:String = NSHomeDirectory() + "/Documents/1.png" 51 //创建文件管理器 52 var fileManager:NSFileManager = NSFileManager.defaultManager() 53 fileManager.moveItemAtPath(locationPath!, toPath: documnets, error: nil) 54 println("new location:\(documnets)") 55 } 56 57 //下载代理方法,监听下载进度 58 func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, 59 didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) { 60 //获取进度 61 var written:CGFloat = (CGFloat)(totalBytesWritten) 62 var total:CGFloat = (CGFloat)(totalBytesExpectedToWrite) 63 var pro:CGFloat = written/total 64 println("下载进度:\(pro)") 65 } 66 67 //下载代理方法,下载偏移 68 func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, 69 didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) { 70 //下载偏移,主要用于暂停续传 71 } 72 73 override func didReceiveMemoryWarning() { 74 super.didReceiveMemoryWarning() 75 } 76 }
1 func sessionUpload(){ 2 //上传地址 3 var url = NSURL(string: "http://hangge.com/") 4 //请求 5 var request:NSURLRequest = NSURLRequest(URL: url!) 6 7 let session = NSURLSession.sharedSession() 8 9 //上传数据流 10 let documents = NSHomeDirectory() + "/Documents/1.png" 11 var imgData = NSData(contentsOfFile: documents) 12 13 var uploadTask = session.uploadTaskWithRequest(request, fromData: imgData) { 14 (data:NSData!, response:NSURLResponse!, error:NSError!) -> Void in 15 //上传完毕后 16 println("上传完毕") 17 } 18 19 //使用resume方法启动任务 20 uploadTask.resume() 21 }
iOS开发——网络Swift篇&NSURLSession加载数据、下载、上传文件
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4674716.html