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("下载完成")
{
print("正在下载 \(totalBytesWritten)/\(totalBytesExpectedToWrite)")
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()
}
// 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,69)"> 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,69)"> print("上传成功响应\(Response)")
overrIDe func dIDReceiveMemoryWarning() {
super.dIDReceiveMemoryWarning()
// dispose of any resources that can be recreated.
}
总结以上是内存溢出为你收集整理的swift网络请求的相关方法全部内容,希望文章能够帮你解决swift网络请求的相关方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)