swift网络请求的相关方法
来源:互联网 发布:做结构设计的基本软件 编辑:程序博客网 时间:2024/06/05 06:03
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.
}
}
- swift网络请求的相关方法
- swift网络数据请求方法
- swift网络数据请求方法
- swift网络数据的请求
- Swift解析网络请求的数据
- Swift的JFNetworkTool网络请求工具类
- Swift版的网络请求封装
- swift 面向协议的网络请求封装
- Swift之网络请求
- swift 网络请求 2
- Swift网络请求
- swift 网络请求
- Swift - 网络请求
- swift 网络请求
- swift网络请求封装
- 网络请求的封装方法
- NSURL网络请求相关的错误
- iOS的网络请求相关知识
- Hive内表与外表
- Road
- 深入理解jQuery插件开发
- android获取手机配置具体代码
- JSP连接MySQL时出现--错误:Access denied for user 'root'@'localhost' (using password: YES)'解决方案
- swift网络请求的相关方法
- git pull Checkout conflict with files:
- iOS设置view的边框、圆角、颜色
- 【DOM学习笔记-Unit03】
- 极光推送初遇
- npoi导出excel中左上角有绿色三角的解决方案和npoi的相关设置
- 实训—学生成绩管理系统
- Android Volley获取本地缓存
- mapreduce优化