iOS URL系统概述:
The URL loading system includes classes that load URLs along with a number of important helper classes that work with those URL loading classes to modify their behavior. The major helper classes fall into five categories: protocol support, authentication and credentials, cookie storage, configuration management, and cache management.(URL加载系统包括URL加载类和一些辅助类,这些辅助类完善URL加载类的操作)
1.URL支持的协议:
The URL loading system provides support for accessing resources using the following protocols:ftp:,http:,https:,data:,file:
It also transparently supports both proxy servers and SOCKS gateways using the user’s system preferences.
(URL:统一资源定位符(uniform resource locator),是在网络中定位资源位置的表示方法.
组成:协议/服务器/路径/文件名;协议(schema)包括http,https,ftp,mail,file,telnet等)
2.URL加载类的区别
- Using
the NSURLSessionclass to asynchronously fetch the contents of a URL tomemoryor download files todiskiniOS 7and later orOS X v10.9and later - Using
NSURLConnectionto asynchronously fetch the contents of a URL tomemoryin older versions ofiOS or OS X. - Using
NSURLDownloadto download files asynchronously todiskin older versions ofOS X.NSURLSession
1.NSURLSession Class
NSURLSession—A session object.NSURLSessionConfiguration—A configuration object used when initializing the session.NSURLSessionTask—The base class for tasks within a session.- :
NSURLSessionDataTask—A task for retrieving the contents of a URL as an NSData object - ::
NSURLSessionUploadTask—A task for uploading a file, then retrieving the contents of a URL as an NSData object - :
NSURLSessionDownloadTask—A task for retrieving the contents of a URL as a temporary file on diskNSURLSession Protocol
NSURLSessionDelegate- :
NSURLSessionTaskDelegate<NSURLSessionDelegate> - ::
NSURLSessionDataDelegate<NSURLSessionTaskDelegate> - ::
NSURLSessionDownloadDelegate<NSURLSessionTaskDelegate>2.Use NSURLSession
1) Create a session configuration.
1
[NSURLSessionConfiguration defaultSessionConfiguration]
2) Create a session, specifying a configuration object and, optionally, a delegate.
1
2
3
4sessionWithConfiguration:
//根据创建的Configuration创建一个Session,系统默认创建一个新的OperationQueue处理Session的消息。
sessionWithConfiguration:delegate:delegateQueue:
//设定回调的delegate(这个delegate会被强引用),并设定delegate在哪个OperationQueue回调,如果设置为[NSOperationQueue mainQueue]就能在主线程进行回调非常的方便。3) Create task objects within a
sessionthat each represent a resourcerequest1
2
3
4
5- (NSURLSessionDataTask *)dataTaskWithRequest:(NSURLRequest *)request;
- (NSURLSessionDataTask *)dataTaskWithURL:(NSURL *)url;
- (NSURLSessionDownloadTask *)downloadTaskWithURL:(NSURL *)url;
- (NSURLSessionDownloadTask *)downloadTaskWithResumeData:(NSData *)resumeData;
NSURLSessionDownloadTask *task = [NSURLSession downloadTaskWithURL:url]4) Call its delegate methods.
1
2
3
4
5
6
7
8
9//NSURLSessionTaskDelegate
- (void)URLSession:task:didCompleteWithError:
//NSURLSessionDataDelegate
- (void)URLSession:dataTask:didReceiveResponse:completionHandler:
- (void)URLSession:dataTask:didReceiveData:
//NSURLSessionDownloadDelegate
- (void)URLSession:downloadTask:didFinishDownloadingToURL:
- (void)URLSession:downloadTask:didWriteData:totalBytesWritten:totalBytesExpectedToWrite:
- (void)URLSession:downloadTask:didResumeAtOffset:expectedTotalBytes:
3.NSURLSession断点续传
1 | #import "ViewController.h" |


