第25月25日 urlsession
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的更多相关文章
- 2016年12月30日 星期五 --出埃及记 Exodus 21:25
2016年12月30日 星期五 --出埃及记 Exodus 21:25 burn for burn, wound for wound, bruise for bruise.以烙还烙,以伤还伤,以打还打 ...
- 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 ...
- HDU(4528),BFS,2013腾讯编程马拉松初赛第五场(3月25日)
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4528 小明系列故事——捉迷藏 Time Limit: 500/200 MS (Java/O ...
- 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 ...
- 2016年11月25日 星期五 --出埃及记 Exodus 20:16
2016年11月25日 星期五 --出埃及记 Exodus 20:16 "You shall not give false testimony against your neighbor.不 ...
- 2016年11月9日 星期三 --出埃及记 Exodus 19:25
2016年11月9日 星期三 --出埃及记 Exodus 19:25 So Moses went down to the people and told them.于是摩西下到百姓那里告诉他们.
- 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 ...
- 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 ...
- 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 ...
随机推荐
- A1137. Final Grading
For a student taking the online course "Data Structures" on China University MOOC (http:// ...
- 【CF1119D】Frets On Fire
题目大意:给定一个长度为 n 的序列,给定一个恒定的 w,求解 \[\sum\limits_{i=1}^{n}min\{d[i],w\}\] 题解:学会了对最小值和式的快速处理. 若在下标的角度考虑, ...
- log4j 配置,tomcat 启动或有后台操作时,控制台会显示很多 DEBUG 信息
log4j 配置,tomcat 启动或有后台操作时,控制台会显示很多 DEBUG 信息 日志信息可以以文件形式显示,也可以在控制台输出,在 log4j.properties 文件设置. 控制台有很多 ...
- jQuery中mouseleave和mouseout的区别详解
很多人在使用jQuery实现鼠标悬停效果时,一般都会用到mouseover和mouseout这对事件.而在实现过程中,可能会出现一些不理想的状况. 先看下使用mouseout的效果: <p> ...
- node(基础)_node中的javascript
一.前言 ...
- Linux设备树(二 节点)
二 节点(node)的表示 首先说节点的表示方法,除了根节点只用一个斜杠“/”表示外,其他节点的表示形式如“node-name@unit-address”.@前边是节点名字,后边是节点地址.节点名字的 ...
- layui基础总结
1.layui结构 ├─css //css目录 │ │─modules //模块css目录(一般如果模块相对较大,我们会单独提取,比如下面三个:) │ │ ├─laydate ...
- 安装pycrypto2.6.1报错
C:\Users\xxx\Downloads\pycrypto-2.6.1\pycrypto-2.6.1>python setup.py install running install runn ...
- Java开发中的编码分析__GET&POST
GET方式提交参数分析 code.jsp <%@ page language="java" contentType="text/html; charset=UTF- ...
- linux shell变量的截取
变量的截断,经常用到的是${},##和%%几个特殊符号.假设我们定义了一个变量为:file=/dir1/dir2/dir3/my.file.txt ,可以用${ }分别替换得到不同的值: ${file ...