NSURLConnection,NSURLSession

https://github.com/Alamofire/Alamofire

NSURLConnection 与 NSURLSession

NSURLConnection 指的是一组构成Foundation框架中URL加载系统的相互关联的组件:NSURLRequest,NSURLResponse,NSURLProtocol,NSURLCache,NSHTTPCookieStorage,NSURLCredentialStorage,以及和它同名的NSURLConnection。

在WWDC 2013中,Apple的团队对NSURLConnection进行了重构,并推出了NSURLSession作为替代。

NSURLSession也是一组相互依赖的类,它的大部分组件和NSURLConnection中的组件相同如NSURLRequest,NSURLCache等。而NSURLSession的不同之处在于,它将NSURLConnection替换为NSURLSession和NSURLSessionConfiguration,以及3个NSURLSessionTask的子类:NSURLSessionDataTask, NSURLSessionUploadTask, 和NSURLSessionDownloadTask。

NSURLSessionConfiguration

//会话模式
//默认会话模式(default):工作模式类似于原来的NSURLConnection,使用的是基于磁盘缓存的持久化策略,使用用户keychain中保存的证书进行认证授权。
class func defaultSessionConfiguration() -> NSURLSessionConfiguration
//瞬时会话模式(ephemeral):该模式不使用磁盘保存任何数据。所有和会话相关的caches,证书,cookies等都被保存在RAM中,因此当程序使会话无效,这些缓存的数据就会被自动清空。
class func ephemeralSessionConfiguration() -> NSURLSessionConfiguration
@availability(iOS, introduced=8.0)
//后台会话模式(background):该模式在后台完成上传和下载,在创建Configuration对象的时候需要提供一个NSString类型的ID用于标识完成工作的后台会话。
class func backgroundSessionConfigurationWithIdentifier(identifier: String) -> NSURLSessionConfiguration

//属性
var allowsCellularAccess: Bool  //指定是否允许使用蜂窝连接
var discretionary: Bool            //系统判断(后台传输数据宜选择此项)

 NSURLSession

    class func sharedSession() -> NSURLSession

    init(configuration: NSURLSessionConfiguration) -> NSURLSession
    init(configuration: NSURLSessionConfiguration?, delegate: NSURLSessionDelegate?, delegateQueue queue: NSOperationQueue?) -> NSURLSession

 NSURLSessionTask

NSURLSessionTask是一个抽象子类,它有三个子类:NSURLSessionDataTask,NSURLSessionUploadTask和NSURLSessionDownloadTask。这三个类封装了现代应用程序的三个基本网络任务:获取数据,比如JSON或XML,以及上传和下载文件。

获取实例的方法在 NSURLSession 中

    /* Creates a data task with the given request.  The request may have a body stream. */
    func dataTaskWithRequest(request: NSURLRequest) -> NSURLSessionDataTask
    
    /* Creates a data task to retrieve the contents of the given URL. */
    func dataTaskWithURL(url: NSURL) -> NSURLSessionDataTask
    
    /* Creates an upload task with the given request.  The body of the request will be created from the file referenced by fileURL */
    func uploadTaskWithRequest(request: NSURLRequest, fromFile fileURL: NSURL) -> NSURLSessionUploadTask
    
    /* Creates an upload task with the given request.  The body of the request is provided from the bodyData. */
    func uploadTaskWithRequest(request: NSURLRequest, fromData bodyData: NSData?) -> NSURLSessionUploadTask
    
    /* Creates an upload task with the given request.  The previously set body stream of the request (if any) is ignored and the URLSession:task:needNewBodyStream: delegate will be called when the body payload is required. */
    func uploadTaskWithStreamedRequest(request: NSURLRequest) -> NSURLSessionUploadTask
    
    /* Creates a download task with the given request. */
    func downloadTaskWithRequest(request: NSURLRequest) -> NSURLSessionDownloadTask
    
    /* Creates a download task to download the contents of the given URL. */
    func downloadTaskWithURL(url: NSURL) -> NSURLSessionDownloadTask
    
    /* Creates a download task with the resume data.  If the download cannot be successfully resumed, URLSession:task:didCompleteWithError: will be called. */
    func downloadTaskWithResumeData(resumeData: NSData) -> NSURLSessionDownloadTask

 

@availability(iOS, introduced=7.0)
    func dataTaskWithRequest(request: NSURLRequest, completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionDataTask
    @availability(iOS, introduced=7.0)
    func dataTaskWithURL(url: NSURL, completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionDataTask
    
    /*
     * upload convenience method.
     */
    @availability(iOS, introduced=7.0)
    func uploadTaskWithRequest(request: NSURLRequest, fromFile fileURL: NSURL, completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionUploadTask
    @availability(iOS, introduced=7.0)
    func uploadTaskWithRequest(request: NSURLRequest, fromData bodyData: NSData?, completionHandler: ((NSData!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionUploadTask
    
    /*
     * download task convenience methods.  When a download successfully
     * completes, the NSURL will point to a file that must be read or
     * copied during the invocation of the completion routine.  The file
     * will be removed automatically.
     */
    @availability(iOS, introduced=7.0)
    func downloadTaskWithRequest(request: NSURLRequest, completionHandler: ((NSURL!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionDownloadTask
    @availability(iOS, introduced=7.0)
    func downloadTaskWithURL(url: NSURL, completionHandler: ((NSURL!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionDownloadTask
    @availability(iOS, introduced=7.0)
    func downloadTaskWithResumeData(resumeData: NSData, completionHandler: ((NSURL!, NSURLResponse!, NSError!) -> Void)?) -> NSURLSessionDownloadTask

 

    func finishTasksAndInvalidate()
    
    /* -invalidateAndCancel acts as -finishTasksAndInvalidate, but issues
     * -cancel to all outstanding tasks for this session.  Note task 
     * cancellation is subject to the state of the task, and some tasks may
     * have already have completed at the time they are sent -cancel. 
     */
    func invalidateAndCancel()
    
    func resetWithCompletionHandler(completionHandler: () -> Void) /* empty all cookies, cache and credential stores, removes disk files, issues -flushWithCompletionHandler:. Invokes completionHandler() on the delegate queue if not nil. */
    func flushWithCompletionHandler(completionHandler: () -> Void) /* flush storage to disk and clear transient network caches.  Invokes completionHandler() on the delegate queue if not nil. */
    
    func getTasksWithCompletionHandler(completionHandler: ([AnyObject]!, [AnyObject]!, [AnyObject]!) -> Void) /* invokes completionHandler with outstanding data, upload and download tasks. */
   

NSOperationQueue和NSOperation

array add block

iOS7 Networking with NSURLSession: Part 4

iOS7 Networking with NSURLSession: Part 3

iOS7 Networking with NSURLSession: Part 2

iOS7 Networking with NSURLSession: Part 1

NSURLConnection

NSHTTPCookieStorage

NSURLSession学习笔记(三)Download Task

NSURLSession学习笔记(二)Session Task

NSURLSession学习笔记(一)简介
AFNetworking 2.0

NSURLCache

 

 

 

Alamofire

AlamofireErrorDomain

Method

ParameterEncoding

 

Manager 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。