swift网络请求的相关方法
2015-12-25 12:36
447 查看
import UIKit
class DownLocader: NSObject,NSURLSessionDownloadDelegate {
var session: NSURLSession?
override init() {
super.init()
let imageURL = NSURL(string: "https://httpbin.org/image/png")!
session = NSURLSession(configuration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("taask"), delegate: self, delegateQueue: nil)
session?.downloadTaskWithURL(imageURL).resume()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL){
print("下载完成")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
{
print("正在下载 \(totalBytesWritten)/\(totalBytesExpectedToWrite)")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64)
{
print("从 \(fileOffset)
处恢复下载,一共 \(expectedTotalBytes)")
}
}
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//网络请求
var dow: DownLocader
dow = DownLocader()
if let url = NSURL(string: "https://httpbin.org/get"){
NSURLSession.sharedSession().dataTaskWithURL(url
, completionHandler: { (let data, let Response, let error) -> Void in
print(Response)
}).resume()
}
let imageURL = NSURL(string: "https://httpbin.org/image/png")!
// NSURLSession.sharedSession().downloadTaskWithURL(imageURL) {Loca, response, error) -> Void in
//
//
// guard let url = Loca else { return }
// guard let imageData = NSData(contentsOfURL: url) else { return }
// guard let image = UIImage(data: imageData) else { return }
//
// }.resume()
NSURLSession.sharedSession().downloadTaskWithURL(imageURL) { (Loca, response, error) -> Void in
guard let url = Loca else { return }
guard let imageData = NSData(contentsOfURL: url) else { return }
guard let image = UIImage(data: imageData) else { return }
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.imageView.image=image;
})
}.resume()
let uploadURL = NSURL(string: "https://httpbin.org/image/png")!
let request = NSURLRequest(URL: uploadURL)
let fileURL = NSURL(fileURLWithPath: "pathToUpload")
NSURLSession.sharedSession().uploadTaskWithRequest(request, fromFile: fileURL) { (let data, let Response, let error) -> Void in
print("上传成功响应\(Response)")
}.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
class DownLocader: NSObject,NSURLSessionDownloadDelegate {
var session: NSURLSession?
override init() {
super.init()
let imageURL = NSURL(string: "https://httpbin.org/image/png")!
session = NSURLSession(configuration: NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("taask"), delegate: self, delegateQueue: nil)
session?.downloadTaskWithURL(imageURL).resume()
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didFinishDownloadingToURL location: NSURL){
print("下载完成")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)
{
print("正在下载 \(totalBytesWritten)/\(totalBytesExpectedToWrite)")
}
func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64)
{
print("从 \(fileOffset)
处恢复下载,一共 \(expectedTotalBytes)")
}
}
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
//网络请求
var dow: DownLocader
dow = DownLocader()
if let url = NSURL(string: "https://httpbin.org/get"){
NSURLSession.sharedSession().dataTaskWithURL(url
, completionHandler: { (let data, let Response, let error) -> Void in
print(Response)
}).resume()
}
let imageURL = NSURL(string: "https://httpbin.org/image/png")!
// NSURLSession.sharedSession().downloadTaskWithURL(imageURL) {Loca, response, error) -> Void in
//
//
// guard let url = Loca else { return }
// guard let imageData = NSData(contentsOfURL: url) else { return }
// guard let image = UIImage(data: imageData) else { return }
//
// }.resume()
NSURLSession.sharedSession().downloadTaskWithURL(imageURL) { (Loca, response, error) -> Void in
guard let url = Loca else { return }
guard let imageData = NSData(contentsOfURL: url) else { return }
guard let image = UIImage(data: imageData) else { return }
dispatch_async(dispatch_get_main_queue(), { () -> Void in
self.imageView.image=image;
})
}.resume()
let uploadURL = NSURL(string: "https://httpbin.org/image/png")!
let request = NSURLRequest(URL: uploadURL)
let fileURL = NSURL(fileURLWithPath: "pathToUpload")
NSURLSession.sharedSession().uploadTaskWithRequest(request, fromFile: fileURL) { (let data, let Response, let error) -> Void in
print("上传成功响应\(Response)")
}.resume()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
相关文章推荐
- Java基础--网络编程01
- Linux下更改TCP window size, MSS, RTT
- iOS获取网络图片的宽、高
- HttpServletResponse addHeader() 与 setHeader() 区别
- Ubuntu有线网络无法连接—有线网络连接图标变为灰色,无法点击
- 计算机网络常见面试题总结
- Network学习3_CentOS系统下向/etc/sysconfig/network-scripts/目录添加ifcfg-Auto_em1来配置网络连接
- 离职了,网络请求工具类发出来,大家需要的拿走,Xutils的
- 【HDU】4888 Redraw Beautiful Drawings 网络流【推断解是否唯一】
- linux配置网络
- 利用AFNetworking监听网络状态
- 极光推送技术原理:移动无线网络长连接
- 聊聊HTTPS和SSL/TLS协议
- 多个虚拟机网络互通
- Android 使用OkHttp
- 【iCore3 双核心板】例程二十一:LAN_TCPS实验——以太网数据传输
- Let's Encrypt,免费好用的 HTTPS 证书
- Let's Encrypt,免费好用的 HTTPS 证书
- HTTP笔记(四)Cookie
- android 后台报错com.lidroid.xutils.exception.HttpException: Not Found-----Not Found 解决办法