ARC下采用的第三方库网络下载&nbsp…

需要借助第三方库AFNetworking


#import "ViewController.h"

#import "AFNetworking.h"


@interface ViewController (){

    UIImageView* _imageView;

    UIProgressView* _pv;

    AFImageRequestOperation* _operation;

}


@end


@implementation ViewController


- (void)viewDidLoad

{

    [super viewDidLoad];

    //断点续传 请求队列

    

    //[self httpRequest];

    //[self jsonRequest];

    //[self imageRequest];

    //[self client];

    [self makeView];

}


- (void)makeView{

    _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 240)];

    [self.view addSubview:_imageView];


    _pv = [[UIProgressView alloc] initWithFrame:CGRectMake(50, 300, 220, 20)];

    [self.view addSubview:_pv];

    

    NSArray* array = @[@"开始", @"暂停", @"继续"];

    for (int i = 0; i < 3; i++) {

        UIButton* button = [UIButton buttonWithType:UIButtonTypeRoundedRect];

        button.frame = CGRectMake(20 + 100 * i, 350, 80, 20);

        [button setTitle:array[i] forState:UIControlStateNormal];

        button.tag = i;

        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:button];

    }

    

    //请求

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://img0.bdstatic.com/img/image/shouye/mxtfboy-13712786966.jpg"]];

    

    _operation = [[AFImageRequestOperation alloc] initWithRequest:request];

    //指定下载目录

    NSString* path = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/a.jpg"];

    NSLog(@"%@", path);

    NSURL* url = [NSURL fileURLWithPath:path];

    _operation.outputStream = [NSOutputStream outputStreamWithURL:url append:NO];

    //回调

    [_operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSLog(@"下载完成");

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败");

    }];

    //进度条

    __weak UIProgressView* pv = _pv;

    [_operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) {

        [pv setProgress:(float)totalBytesRead/totalBytesExpectedToRead animated:YES];

    }];


}


- (void)buttonClick:(UIButton*)button{

    //开始

    if (button.tag == 0) {

        [_operation start];

    }

    //暂停

    if (button.tag == 1) {

        [_operation pause];

    }

    //继续

    if (button.tag == 2) {

        [_operation resume];

    }

}


- (void)client{

   

    AFHTTPClient* client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://10.0.8.8/sns/"]];

    //发起一个get请求

    [client getPath:@"my/user_list.php?page=1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {

        

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败");

    }];

    //发起一个post请求

    [client postPath:@"my/user_list.php" parameters:@{@"page":@"1"} success:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"%@", str);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败");

    }];

}


- (void)imageRequest{

    

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.baidu.com/img/bdlogo.png"]];

    

    AFImageRequestOperation* operation = [[AFImageRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        self.view.backgroundColor = [UIColor colorWithPatternImage:responseObject];

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败");

    }];

    [operation start];

}


- (void)jsonRequest{

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"https://api.douban.com/v2/book/search?q=harry&start=0&apikey=08e5e8516f3402a71ab1b8f44d9c07f5"]];

    AFJSONRequestOperation* operation = [[AFJSONRequestOperation alloc] initWithRequest:request];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSNumber* num = [responseObject objectForKey:@"count"];

        NSLog(@"%@", num);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败");

    }];

    [operation start];

}


- (void)httpRequest{

    

    NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://10.0.8.8/sns/my/user_list.php"]];

    

    AFHTTPRequestOperation* operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

    //设置回调block

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        NSString* str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSLog(@"%@", str);

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        NSLog(@"请求失败");

    }];

    //开始

    [operation start];

}








@end


你可能感兴趣的:(ARC下采用的第三方库网络下载&nbsp…)