**@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