1.

private lazy var session: URLSession = {
let configuration = URLSessionConfiguration.default
configuration.waitsForConnectivity = true
return URLSession(configuration: configuration,
delegate: self, delegateQueue: nil)
}()

Listing 3

Using a delegate with a URL session data task

var receivedData: Data?

func startLoad() {
loadButton.isEnabled = false
let url = URL(string: "https://www.example.com/")!
receivedData = Data()
let task = session.dataTask(with: url)
task.resume()
} // delegate methods func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive response: URLResponse,
completionHandler: @escaping (URLSession.ResponseDisposition) -> Void) {
guard let response = response as? HTTPURLResponse,
(...).contains(response.statusCode),
let mimeType = response.mimeType,
mimeType == "text/html" else {
completionHandler(.cancel)
return
}
completionHandler(.allow)
} func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) {
self.receivedData?.append(data)
} func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
DispatchQueue.main.async {
self.loadButton.isEnabled = true
if let error = error {
handleClientError(error)
} else if let receivedData = self.receivedData,
let string = String(data: receivedData, encoding: .utf8) {
self.webView.loadHTMLString(string, baseURL: task.currentRequest?.url)
}
}
}

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

===============================================

1.

12.If a download task completes successfully, then the NSURLSession object calls the task’s URLSession:downloadTask:didFinishDownloadingToURL: method with the location of a temporary file. Your app must either read the response data from this file or move it to a permanent location before this delegate method returns.

https://developer.apple.com/documentation/foundation/nsurlsession?language=objc

https://www.jianshu.com/p/8790684782c3

2.

后台任务完成操作

在切到后台之后,Session的Delegate不会再收到,Task相关的消息,直到所有Task全都完成后,系统会调用ApplicationDelegate的application:handleEventsForBackgroundURLSession:completionHandler:回调,把通知App后台Task已经完成,这里你需要completionHandler的bloack存起来下面会用到,接着每一个后台的Task就会调用Session的Delegate中的URLSession:downloadTask:didFinishDownloadingToURL:和URLSession:task:didCompleteWithError: 。

各个Task在Session的delegate调用之后,最后会调用Session的Delegate回调URLSessionDidFinishEventsForBackgroundURLSession:。这个时候你可以调用上面保存completionHandler的bloack,告知系统你的后台任务已经完成,系统可以安全的休眠你的App。

https://blog.csdn.net/hxming22/article/details/79392880

https://github.com/nsscreencast/093-background-transfers

3.

AFURLSessionManager

- (void)setDidFinishEventsForBackgroundURLSessionBlock:(void (^)(NSURLSession *session))block {
self.didFinishEventsForBackgroundURLSession = block;
}
。。。
- (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
if (self.didFinishEventsForBackgroundURLSession) {
dispatch_async(dispatch_get_main_queue(), ^{
self.didFinishEventsForBackgroundURLSession(session);
});
}
}

第25月25日 urlsession的更多相关文章

  1. 2016年12月30日 星期五 --出埃及记 Exodus 21:25

    2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...

  2. 2016年12月25日 星期日 --出埃及记 Exodus 21:20

    2016年12月25日 星期日 --出埃及记 Exodus 21:20 "If a man beats his male or female slave with a rod and the ...

  3. HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...

  4. 2016年12月4日 星期日 --出埃及记 Exodus 20:25

    2016年12月4日 星期日 --出埃及记 Exodus 20:25 If you make an altar of stones for me, do not build it with dress ...

  5. 2016年11月25日 星期五 --出埃及记 Exodus 20:16

    2016年11月25日 星期五 --出埃及记 Exodus 20:16 "You shall not give false testimony against your neighbor.不 ...

  6. 2016年11月9日 星期三 --出埃及记 Exodus 19:25

    2016年11月9日 星期三 --出埃及记 Exodus 19:25 So Moses went down to the people and told them.于是摩西下到百姓那里告诉他们.

  7. 2016年10月25日 星期二 --出埃及记 Exodus 19:9

    2016年10月25日 星期二 --出埃及记 Exodus 19:9 The LORD said to Moses, "I am going to come to you in a dens ...

  8. 2016年10月14日 星期五 --出埃及记 Exodus 18:25

    2016年10月14日 星期五 --出埃及记 Exodus 18:25 He chose capable men from all Israel and made them leaders of th ...

  9. 2016年6月28日 星期二 --出埃及记 Exodus 14:25

    2016年6月28日 星期二 --出埃及记 Exodus 14:25 He made the wheels of their chariots come off so that they had di ...

随机推荐

  1. apache ArrayUtils 工具类

    org.apache.commons.lang3.ArrayUtils // 1.add():将给定的数据添加到指定的数组中,返回一个新的数组. int[] arr = { 1, 2, 3 }; in ...

  2. 1062.Talent and Virtue

    About 900 years ago, a Chinese philosopher Sima Guang wrote a history book in which he talked about ...

  3. python 当前时间获取方法

    1.先导入库:import datetime 2.获取当前日期和时间:now_time = datetime.datetime.now() 3.格式化成我们想要的日期:strftime() 比如:“2 ...

  4. flask Blueprint蓝图

    首先要了解蓝图的作用,模拟场景在团队开发过程中团队每个人都在写自己负责的功能模块,那多个py文件模板,我们如果完成后需要运行是不是要运行多个服务?但是我们的项目是一个整体,而不是零散的,所以我们怎么把 ...

  5. io系列之字节流

    java中io流系统庞大,知识点众多,作为小白通过五天的视频书籍学习后,总结了io系列的随笔,以便将来复习查看. 本篇为此系列随笔的第一篇:io系列之字节流. 一.字节流的File读写操作. Inpu ...

  6. socket编程 ------ UDP服务器

    void vLANcommunication( void *pvParameters ) { int32 listenfd; do{ listenfd = socket(AF_INET, SOCK_D ...

  7. 苹果笔记本适合什么人 中国Mac电脑用户的8个事实

    报告由腾讯 ISUX 研究中心收集了全国 7946 名 Mac 电脑用户的问卷整理而成.并且,参考了苹果公司的历年财报,以及百度.StatCounter 等第三方市场统计数据. 你是 iPhone 用 ...

  8. AtomicInteger类的理解与使用

    AtomicInteger类的理解与使用 首先看两段代码,一段是Integer的,一段是AtomicInteger的,为以下: public class Sample1 { private stati ...

  9. 1042. Shuffling Machine (20)

    Shuffling is a procedure used to randomize a deck of playing cards. Because standard shuffling techn ...

  10. MacOS环境中 python3 部署

    MacOS环境中 python3 部署 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.在MacOS安装Python3.6 1>.打开python关于MacOS版本的官方网 ...