当前位置:首页 > Web开发 > 正文

URLSessionDownloadTask : 是一个 下载数据 的 URLSessionTask 的子类

2024-03-31 Web开发

标签:

URLSession 是什么

URL Loading System供给了访谒URL资源的系统,供给了访谒http/https/自界说URL访谒的接口。此中,URLSession实例可以创建多个URLSessionTask实例,完成差此外事情。

我们可以将URLSession类理解为会话层,用于打点网络接口的创建、维护、删除等等事情,我们要做的事情也只是会话层之后的层。

技术图片

URLSessionDataTask : 一个 数据请求 的 URLSessionTask 的子类,用于数据的请求。

URLSessionUploadTask : 是一个 上传数据 的 URLSessionDataTask 的子类。

URLSessionDownloadTask : 是一个下载数据 的 URLSessionTask 的子类。

URLSessionStreamTask : 是一个双向会话的 URLSessionTask 的子类。

根本使用

这里上传下载的都是一个字符串;实际形式是json(需要把NSData转成Json)

get

let url1 = "https://coconutnut.xyz:2019/tumble" let sessionConfigure = URLSessionConfiguration.default // sessionConfigure.httpAdditionalHeaders = ["Content-Type": "application/json"] // sessionConfigure.timeoutIntervalForRequest = 30 sessionConfigure.requestCachePolicy = .reloadIgnoringLocalCacheData let session = URLSession(configuration: sessionConfigure) guard let url = URL(string: url1) else { return } let url1Request = URLRequest(url: url) let dataTask1 = session.dataTask(with: url1Request) { (data, response, error) in guard data != nil else {return} let str = String(data: data!, encoding: String.Encoding.utf8) // 这里需要再加一个逻辑判断 guard let strnow = str else{return}

最后返回的strnow就是我们要获得的字符串;

post

let order = Qumot(value: "1") guard let uploadData = try? JSONEncoder().encode(order) else { return } print(uploadData) let url = URL(string: "https://coconutnut.xyz:2019/qumot")! var request = URLRequest(url: url) request.httpMethod = "POST" request.setValue("application/json", forHTTPHeaderField: "Content-Type") let task = URLSession.shared.uploadTask(with: request, from: uploadData) { data, response, error in if let error = error { print ("error: \(error)") return } guard let response = response as? HTTPURLResponse, (200...299).contains(response.statusCode) else { print ("server error") return } if let mimeType = response.mimeType, mimeType == "application/json", let data = data, let dataString = String(data: data, encoding: .utf8) { print ("got data: \(dataString)") } } task.resume()

使用Delegate

后台唤醒的处所,可以使用本身写的回调函数

可以在这些处所用到闭包

Tasks in a session also share a common delegate object. You implement this delegate to provide and obtain information when various events occur, including when:

Authentication fails.

Data arrives from the server.

Data becomes available for caching.

If you don’t need the features provided by a delegate, you can use this API without providing one by passing nil when you create a session. 

此外这是一个异步的接口,,你可以一边措置惩罚惩罚UI一边访谒网络

Like most networking APIs, the URLSession API is highly asynchronous. It returns data to your app in one of two ways, depending on the methods you call:

By calling a completion handler block when a transfer finishes successfully or with an error.

By calling methods on the session’s delegate as data arrives and when the transfer is complete.

https://developer.apple.com/documentation/foundation/url_loading_system

https://developer.apple.com/documentation/foundation/url_loading_system/uploading_data_to_a_website

工程日记之ChildLost(1):URLSession

标签:

原文地点:https://www.cnblogs.com/Plorde/p/12303043.html

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/30086.html