蘋果的官方文檔是這么介紹NSURLSession這個類的:
NSURLSession is a replacement API for NSURLConnection. It provides options that affect the policy of, and various aspects of the mechanism by which NSURLRequest objects are retrieved from the network.
NSURLSession是一個抽象類竖配,它有三個具體的子類
- NSURLSessionDataTask:獲取數(shù)據(jù),比如JSON或XML
- NSURLSessionUploadTask:上傳文件
- NSURLSessionDownloadTask:下載文件
為什么使用NSURLSession拾稳?
NSURLSession是iOS7中新的網(wǎng)絡接口萨咕,它與咱們熟悉的NSURLConnection是并列的。在程序在前臺時涧团,NSURLSession與NSURLConnection可以互為替代工作撞羽。注意,如果用戶強制將程序關閉箱蝠,NSURLSession會斷掉。
NSURLSession提供的功能
- 通過URL將數(shù)據(jù)下載到內(nèi)存
- 通過URL將數(shù)據(jù)下載到文件系統(tǒng)
- 將數(shù)據(jù)上傳到指定的URL
- 在后臺完成上述功能
工作流程
- 后臺上傳和下載
- 能夠暫停和恢復網(wǎng)絡操作
- Configurable container
- Subclassable and private storage
- Improved authentication handling
- Rich delegate model
- Uploads and downloads through the file system