在Downloader.h文件中
#import <Foundation/Foundation.h>
@class Downloader;
//1. 定义(创建)协议
@protocol DownloaderDataDelegate <NSObject>
//两个可选的方法
@optional
- (void)downloader:(Downloader *)downloader progress:(float)progress;
- (void)downloaderDidFinishLoading:(Downloader *)downloader;
@end
@interface Downloader : NSObject <NSURLConnectionDataDelegate> //挂出代理
@property (nonatomic, copy) NSString *path;
@property (nonatomic, readonly, strong) NSData *data;
//2. 创建一个指针, 用来指向实现该协议的对象
@property (nonatomic, weak) id<DownloaderDataDelegate> delegate;
- (id)initWithPath:(NSString *)path;
- (void)start;
//- (void)pause;
- (void)cancel;
@end
在Downloader.m文件中
#import "Downloader.h"
@interface Downloader ()
{
NSURLConnection *connction;
NSMutableData *_data;
long long totalLength;
}
@end
@implementation Downloader
- (id)initWithPath:(NSString *)path
{
if (self = [super init]) {
self.path = path;
}
return self;
}
- (void)start
{
NSURL *url = [NSURL URLWithString:_path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
connction = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connction start];
}
- (void)cancel
{
[connction cancel];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"%@", error);
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response;
if (httpResponse.statusCode == 200) {
_data = [NSMutableData data];
totalLength = httpResponse.expectedContentLength;
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_data appendData:data];
[_delegate downloader:self progress:(float)_data.length/totalLength];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"downloader finished...: %d", [_data length]);
//3. 使用指针调用协议方法
[_delegate downloaderDidFinishLoading:self];
}
@end
在"ViewController.m"中实现
#import "SecondViewController.h"
#import "Downloader.h"
@interface SecondViewController () <DownloaderDataDelegate>
@end
@implementation SecondViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
Downloader *downloader = [[Downloader alloc] initWithPath:@"http://class.room/hdmv.mp4"];
// downloader.path = @"http://www.ifeng.com";
downloader.delegate = self;
[downloader start];
Downloader *downloader2 = [[Downloader alloc] init];
downloader2.path = @"http://www.baidu.com";
downloader2.delegate = self;
[downloader2 start];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning]
}
//1. 创建(定义)协议
- (void)downloader:(Downloader *)downloader progress:(float)progress
{
NSLog(@"progress: %.2f", progress);
}
- (void)downloaderDidFinishLoading:(Downloader *)downloader
{
NSString *str = [[NSString alloc] initWithData:downloader.data encoding:NSUTF8StringEncoding];
NSLog(@"<.........>: %@", str);
}
@end