**@Block传值**
**.h**
~~~
#import <Foundation/Foundation.h>
typedef void(^MyBlock)(NSData *);
typedef void(^LengthBlock)(float);
@interface HMTMyCustomNetRequest : NSObject<NSURLConnectionDataDelegate>
@property (nonatomic,retain)NSMutableData * data;
@property (nonatomic,retain)NSURLConnection * connection;
@property (nonatomic,copy)MyBlock finishLoadBlock;
@property (nonatomic,copy)LengthBlock lengthBlock;
// GET请求方式
- (void)requestForGETWithUrl:(NSString *)urlString;
// POST请求方式
- (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data;
// 取消请求
- (void)cancelNSURLConnection;
@end
~~~
**.m**
~~~
#import "HMTMyCustomNetRequest.h"
@interface HMTMyCustomNetRequest (){
NSUInteger leng;
}
@end
@implementation HMTMyCustomNetRequest
- (void)dealloc{
RELEASE_SAFELY(_data);
RELEASE_SAFELY(_connection);
Block_release(_finishLoadBlock);
Block_release(_lengthBlock);
[super dealloc];
}
- (void)requestForGETWithUrl:(NSString *)urlString{
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
/**
* Special Considerations
During the download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled.
*/
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
- (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data{
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
self.connection = [NSURLConnection connectionWithRequest:request delegate:self];
}
#pragma mark - 取消协议异步方法调用(关键关键很关键,不做这一步经常出现崩溃的地方)
- (void)cancelNSURLConnection{
/**
* 如果数据在加载,但是因为其他一些原因,比如跳转页面做其他事,而不是正常的数据加载完成或者数据加载失败
* 一定要手动取消网络请求连接
* Special Considerations
* During the download the connection maintains a strong reference to the delegate. It releases that strong reference when the--
* ---connection finishes loading, fails, or is canceled.
* self.connection = [NSURLConnection connectionWithRequest:self delegate:self];
*
*/
[_connection cancel];
self.connection = nil;
}
#pragma mark - 服务器开始给客户端回传数据,这个方法只会执行一次
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
// 初始化接收数据的空data
self.data = [NSMutableData data];
// 取得数据总长度
leng = response.expectedContentLength;
}
#pragma mark - 客户端持续接收数据,data是数据片段,整个数据分段返回,这个方法执行的次数取决于数据总长度[response expectedContentLength]
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.data appendData:data];
float progressLength = [self.data length] / (float)leng;
if (_lengthBlock) {
self.lengthBlock(progressLength);
}
//if (_finishLoadBlock) {
//self.finishLoadBlock(_data);
//}
}
#pragma mark - 数据完全下载成功,接收到完整数据
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
// 调用完整数据的block方法
if (_finishLoadBlock) {
self.finishLoadBlock(_data);
}
}
#pragma mark - 数据下载失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"请求网络出错:%@",error);
}
@end
~~~
**@delegate代理传值**
**.h**
~~~
#import <Foundation/Foundation.h>
@class HMTNetWorkRequest;
@protocol NetWorkRequestDelegate<NSObject>
@optional
//netWork请求成功
- (void)netWorkRequest:(HMTNetWorkRequest *)request didSuccessfulReceiveData:(NSData *)data;
//netWork请求失败
- (void)netWorkRequest:(HMTNetWorkRequest *)request didFailed:(NSError *)error;
//获取netWork的下载进度
- (void)netWorkRequest:(HMTNetWorkRequest *)request withProgress:(CGFloat)progress;
@end
@interface HMTNetWorkRequest : NSObject<NSURLConnectionDataDelegate>
@property (nonatomic,assign) id<NetWorkRequestDelegate> delegate;
@property (nonatomic,retain)NSURLConnection * connection;
- (void)requestForGETWithUrl:(NSString *)urlString;
- (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data;
// 取消网络请求
- (void)cancelRequest;
@end
~~~
**.m**
~~~
#import "HMTNetWorkRequest.h"
@interface HMTNetWorkRequest (){
NSUInteger _totalLength;
}
@property (nonatomic,retain)NSMutableData * receiveData;
@end
@implementation HMTNetWorkRequest
- (void)dealloc{
RELEASE_SAFELY(_connection);
RELEASE_SAFELY(_receiveData);
[super dealloc];
}
#pragma mark - 不用的时候一定要取消
- (void)cancelRequest{
[_connection cancel];
self.connection = nil;
self.delegate = nil;
}
- (void)requestForGETWithUrl:(NSString *)urlString{
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"GET"];
/**
* Special Considerations
During the download the connection maintains a strong reference to the delegate. It releases that strong reference when the connection finishes loading, fails, or is canceled.
*/
[NSURLConnection connectionWithRequest:request delegate:self];
[request release];
}
- (void)requestForPOSTWithUrl:(NSString *)urlString postData:(NSData *)data{
NSURL * url = [NSURL URLWithString:urlString];
NSMutableURLRequest * request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:data];
[NSURLConnection connectionWithRequest:request delegate:self];
[request release];
}
#pragma mark - 服务器开始给客户端回传数据,这个方法只会执行一次
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
// 服务器开始回传数据,客户端需要创建一个空的,可变的Data对象,用于存储每次获取的数据片段
self.receiveData = [NSMutableData data];
// 取得请求数据的长度
_totalLength = [response expectedContentLength];
}
#pragma mark - 客户端持续接收数据,data是数据片段,整个数据分段返回,这个方法执行的次数取决于数据总长度[response expectedContentLength]
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.receiveData appendData:data];
CGFloat progress = [_receiveData length]/_totalLength;
if ([_delegate respondsToSelector:@selector(netWorkRequest:withProgress:)]) {
[_delegate netWorkRequest:self withProgress:progress];
}
}
#pragma mark - 数据完全下载成功,接收到完整数据
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if ([_delegate respondsToSelector:@selector(netWorkRequest:didSuccessfulReceiveData:)]) {
[_delegate netWorkRequest:self didSuccessfulReceiveData:_receiveData];
}
self.receiveData = nil;
}
#pragma mark - 数据下载失败
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
NSLog(@"didFailWithError");
}
@end
~~~
- 前言
- 沙盒机制与文件(一)
- 沙盒机制和文件(二)
- 沙盒机制和文件(三)
- NSBundle介绍以及读取沙盒文件路径问题
- 数据持久化(一)-----归档 读写 文件路径
- 数据持久化(二)-----Sqlite
- 数据持久化(三)使用第三方类库FMDB
- 数据持久化(四)之NSUserDefaults
- 数据持久化(五)之CoreData
- 数据持久化(六)之Using CoreData with MagicalRecord
- 数据解析(一)解析XML之系统自带NSXMLParse类
- 数据解析(二)解析XML之GDataXMLNode
- 数据解析(三)解析JSON-----系统自带NSJSONSerialization 与 第三方JSONKit
- iOS多线程编程(一)NSThread
- iOS多线程编程(二)NSOperationQueue
- iOS多线程编程(三)Grand Central Dispatch(GCD)详解
- iOS网络编程(一)NSURLConnection
- iOS网络编程(二) 自定义请求网络类----推荐用于需要请求过程片段数据
- iOS网络编程(三) 异步加载及缓存图片---->SDWebImage
- iOS网络编程(四) 异步加载及缓存图片-----自定义类
- iOS网络编程(五) 异步加载及缓存图片-----EGO
- iOS网络编程(六) NSURLSession详解
- iOS网络编程(7) 第三方开源库----->AFNetworking