簡單網絡請求封裝

//
//  HTTPRequset.h
//  請求類分裝
//
//  Created by qianfeng on 15-7-26.
//  Copyright (c) 2015年 qq. All rights reserved.
//
//經過代理和block實現函數回調,可選其一
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@class HTTPRequset;
@protocol HTTPRequsetDelegate <NSObject>

@optional
/**
    進度的回調
 */
- (void)requestDidProgress:(CGFloat)progress;
//請求完成的回調
- (void)requestDidfinish:(HTTPRequset *)httprequest;
/**
    請求完成的回調
 */
- (void)requestDidError:(NSError *)error;
@end

/**
    請求類
 */
@interface HTTPRequset : NSObject
/**
    網絡請求返回數據源
 */
@property(nonatomic,strong) NSData * responseData;
/**
    字符串形式
 */
@property(nonatomic,copy ) NSString * reponseString;
/**
    代理
 */
@property(nonatomic,assign)id<HTTPRequsetDelegate>delegate;
/**
 使用block實現回調下載進程
 */
@property(nonatomic,copy) void(^requestDidProgressBlock)(CGFloat progress);
/**
 使用block實現回調對象
 */
@property(nonatomic,copy) void(^requestDidFinishBlock)(HTTPRequset * request);
/**
 使用block實現回調錯誤信息
 */
@property(nonatomic,copy) void(^requestDidErrorBlock)(NSError * error);

/**
 構造方法
 */
- (id)initWithUrl:(NSURL *)url;
/**
    開始網絡請求
 */
- (void)startRequest;
@end
//
//  HTTPRequset.m
//  請求類分裝
//
//  Created by qianfeng on 15-7-26.
//  Copyright (c) 2015年 qq. All rights reserved.
//

#import "HTTPRequset.h"

@interface HTTPRequset ()<NSURLConnectionDataDelegate,NSURLConnectionDelegate>
{
    NSURL * _url;
    //保存下載數據
    NSMutableData * _downloadData;
    
    //總大小
    long long _fileSizeBytes;
}
@end

@implementation HTTPRequset
- (id)initWithUrl:(NSURL *)url
{
    self = [super init];
    if (self) {
        
        _url = url;
        _downloadData = [NSMutableData data];
    }
    return self;
}
- (void)startRequest
{
    NSURLRequest * request = [NSURLRequest requestWithURL:_url];
    [NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark - 代理方法
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    //清空數據
    _downloadData.length = 0;
    //獲取文件總大小
    _fileSizeBytes = [response expectedContentLength];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    //追加
    [_downloadData appendData:data];
    //計算進度
    float progress = (float) _downloadData.length / _fileSizeBytes;
    if ([self.delegate respondsToSelector:@selector(requestDidProgress:)]) {
        [self.delegate requestDidProgress:progress];
    }
    self.requestDidProgressBlock(progress);
    
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //請求取消
    [connection cancel];
    
    //
    _responseData = _downloadData;
    _reponseString = [[NSString alloc]initWithData:_downloadData encoding:NSUTF8StringEncoding];
    self.requestDidFinishBlock(self);
    if ([self.delegate respondsToSelector:@selector(requestDidfinish:)]) {
        [self.delegate requestDidfinish:self];
    }
    
    
    
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //請求取消
    [connection cancel];
    self.requestDidErrorBlock(error);
    if ([self.delegate respondsToSelector:@selector(requestDidError:)]) {
        [self.delegate requestDidError:error];
    }
    
}



@end
相關文章
相關標籤/搜索