对下载进行一个简单的封装

在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




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值