使用方法如上图.
下面直接发代码:
头文件
// // CWLWConnectManager.h // LechaoDrawGuess // // Created by luoge on 12-11-19. // Copyright (c) 2012年 watsy. All rights reserved. // #import <Foundation/Foundation.h> #import "ASIHTTPRequest.h" #import "ASIFormDataRequest.h" typedef enum { connectType_GET = 0, connectType_POST } connectType; @interface CWLWConnectManager : NSObject + (CWLWConnectManager *) sharedInstance; + (void) releaseInstance; //投递方法 - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del; - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del tag:(NSInteger) nTag; - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del tag:(NSInteger) nTag key:(NSString *) sKey; #if NS_BLOCKS_AVAILABLE - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params result:(void(^)(BOOL bSuccess,id returnData,NSError *error)) block; - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params tag:(NSInteger) nTag result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) block; - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params tag:(NSInteger) nTag key:(NSString *) sKey result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block; #endif //取消网络连接 - (BOOL) cancelWithHashValue:(NSUInteger) nItemHashValue; - (BOOL) cancelURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del tag:(NSInteger) nTag key:(NSString *) sKey; @end @interface NSObject(CWLCConnection) - (void) didCWFinishSuccessedWithData:(id) data tag:(NSInteger) nTag key:(NSString *) sKey; - (void) didCWFinishFailedWithError:(NSError *) error tag:(NSInteger) nTag key:(NSString *) sKey; @end
// // CWLWConnectManager.m // LechaoDrawGuess // // Created by luoge on 12-11-19. // Copyright (c) 2012年 watsy. All rights reserved. // #import "CWLWConnectManager.h" static CWLWConnectManager *_pConnectionMgr_ = nil; @interface CWConnItem : NSObject <ASIHTTPRequestDelegate> @property (nonatomic, assign) connectType cType; @property (nonatomic, assign) NSInteger nTag; @property (nonatomic, strong) NSString *sKey; @property (nonatomic, assign) id delegate; @property (nonatomic, strong) NSString *sURL; @property (nonatomic, strong) NSDictionary *params; @property (nonatomic, strong) ASIHTTPRequest *httpReq; + (CWConnItem *) connWithtype:(connectType) ct url:(NSString *) sl params:(NSDictionary *) pd Tag:(NSInteger) nt key:(NSString *) sk delegate:(id) del; - (CWConnItem *) initWithtype:(connectType) ct url:(NSString *) sl params:(NSDictionary *) pd Tag:(NSInteger) nt key:(NSString *) sk blockWithOutTagAndKey:(void(^)(BOOL bSuccess,id returnData,NSError *error)) blockWithOutTagAndKey blockWithOutKey:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) blockWithOutKey block:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block; - (void) start; @end @implementation CWConnItem @synthesize nTag,sKey,delegate,sURL,cType,params; @synthesize httpReq; - (void) dealloc { [httpReq clearDelegatesAndCancel]; [httpReq release]; [params release]; [sURL release]; [sKey release]; [super dealloc]; } + (CWConnItem *) connWithtype:(connectType) ct url:(NSString *) sl params:(NSDictionary *) pd Tag:(NSInteger) nt key:(NSString *) sk delegate:(id) del { CWConnItem *item = [[CWConnItem alloc] init]; item.nTag = nt; item.sKey = sk; item.sURL = sl; item.delegate = del; item.cType = ct; item.params = pd; return [item autorelease]; } - (CWConnItem *) initWithtype:(connectType) ct url:(NSString *) sl params:(NSDictionary *) pd Tag:(NSInteger) nt key:(NSString *) sk blockWithOutTagAndKey:(void(^)(BOOL bSuccess,id returnData,NSError *error)) blockWithOutTagAndKey blockWithOutKey:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) blockWithOutKey block:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block { if (self = [super init]) { self.nTag = nt; self.sKey = sk; self.sURL = sl; self.cType = ct; self.params = pd; if (cType == connectType_GET) { self.httpReq = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:sURL]]; [httpReq setRequestMethod:@"GET"]; } else if (cType == connectType_POST) { self.httpReq = [ASIFormDataRequest requestWithURL:[NSURL URLWithString:sURL]]; [httpReq setRequestMethod:@"POST"]; ASIFormDataRequest *form = (ASIFormDataRequest *) httpReq; for (NSString *paramKey in [params allKeys]) { [form addPostValue:[params objectForKey:paramKey] forKey:paramKey]; } } [httpReq setCompletionBlock:^(void){ //如果没有tag和key if (blockWithOutTagAndKey) { blockWithOutTagAndKey(YES, httpReq.responseString, nil); } //如果没有key if (blockWithOutKey) { blockWithOutKey(YES, httpReq.responseString, nil, nTag); } if (block) { block(YES, httpReq.responseString, nil, self.nTag, self.sKey); } if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) { [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:) withObject:self withObject:nil]; } }]; [httpReq setFailedBlock:^(void) { if (blockWithOutTagAndKey) { blockWithOutTagAndKey(NO, httpReq.responseString, httpReq.error); } if (blockWithOutKey) { blockWithOutKey(NO, httpReq.responseString, httpReq.error, nTag); } if (block) { block(NO, httpReq.responseString, httpReq.error, self.nTag, self.sKey); } if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) { [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:) withObject:self withObject:httpReq.error]; } }]; [httpReq start]; } return self; } - (void) start { NSAssert((sURL != nil), @"url can't be nil"); sURL = [sURL stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; if (cType == connectType_GET) { httpReq = [[ASIHTTPRequest alloc] initWithURL:[NSURL URLWithString:sURL]]; [httpReq setRequestMethod:@"GET"]; } else if (cType == connectType_POST) { httpReq = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:sURL]]; [httpReq setRequestMethod:@"POST"]; ASIFormDataRequest *form = (ASIFormDataRequest *) httpReq; for (NSString *paramKey in [params allKeys]) { [form addPostValue:[params objectForKey:paramKey] forKey:paramKey]; } } httpReq.delegate = self; [httpReq startAsynchronous]; } - (BOOL) isEqual:(id)object { CWConnItem *item = (CWConnItem *)object; if (![self.sURL isEqualToString:item.sURL]) { return NO; } if (self.cType != item.cType) { return NO; } if (self.delegate != item.delegate) { return NO; } if (self.nTag != item.nTag) { return NO; } if (self.sKey != nil && item.sKey != nil && ![self.sKey isEqualToString:item.sKey]) { return NO; } for (NSString *paramKey in self.params) { id sp1 = [self.params objectForKey:paramKey]; id sp2 = [self.params objectForKey:paramKey]; if (sp2 == nil) { return NO; } if (![sp1 isEqual:sp2]) { return NO; } } return YES; } - (void)requestFinished:(ASIHTTPRequest *)request { if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) { [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:) withObject:self withObject:nil]; } } - (void)requestFailed:(ASIHTTPRequest *)request { if([[CWLWConnectManager sharedInstance] respondsToSelector:@selector(didFinishedWithItems:error:)]) { [[CWLWConnectManager sharedInstance] performSelector:@selector(didFinishedWithItems:error:) withObject:self withObject:request.error]; } } @end #pragma mark - CWLWConnectManager @interface CWLWConnectManager() @property (nonatomic, strong) NSMutableDictionary *connItems; //移除元素 - (id) hasItem:(CWConnItem *) item; - (void) removeItems:(CWConnItem *) conn; - (void) didFinishedWithItems:(CWConnItem *) conn error:(NSError *) error; @end @implementation CWLWConnectManager @synthesize connItems; - (id) init { if (_pConnectionMgr_) { return _pConnectionMgr_; } if (self = [super init]) { self.connItems = [NSMutableDictionary dictionary]; } return self; } - (void) dealloc { [connItems release]; [super dealloc]; } + (CWLWConnectManager *) sharedInstance { if (_pConnectionMgr_ == nil) { _pConnectionMgr_ = [[CWLWConnectManager alloc] init]; } return _pConnectionMgr_; } + (void) releaseInstance { if (_pConnectionMgr_ != nil) { [_pConnectionMgr_ release]; _pConnectionMgr_ = nil; } } - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del { return [self conURL:sURL connectType:cType params:params delegate:del tag:0]; } - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del tag:(NSInteger) nTag { return [self conURL:sURL connectType:cType params:params delegate:del tag:nTag key:nil]; } - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del tag:(NSInteger) nTag key:(NSString *) sKey { CWConnItem *item = [CWConnItem connWithtype:cType url:sURL params:params Tag:nTag key:sKey delegate:del]; if ([self hasItem:item]) { //重复调用方法 } NSUInteger hashValue = [item hash]; [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]]; [item start]; return hashValue; } //取消网络连接 - (BOOL) cancelWithHashValue:(NSUInteger) nItemHashValue { CWConnItem *conn = [self.connItems objectForKey:[NSNumber numberWithUnsignedInteger:nItemHashValue]]; if (conn) { [conn.httpReq clearDelegatesAndCancel]; [self.connItems removeObjectForKey:[NSNumber numberWithUnsignedInteger:nItemHashValue]]; } return YES; } - (BOOL) cancelURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params delegate:(id) del tag:(NSInteger) nTag key:(NSString *) sKey { CWConnItem *item = [CWConnItem connWithtype:cType url:sURL params:params Tag:nTag key:sKey delegate:del]; CWConnItem *existItem = [self hasItem:item]; if (existItem != nil) { if (existItem.httpReq != nil) { [existItem.httpReq clearDelegatesAndCancel]; } [self.connItems removeObjectForKey:[NSNumber numberWithUnsignedInteger:[existItem hash]]]; return NO; } return YES; } - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params result:(void(^)(BOOL bSuccess,id returnData,NSError *error)) block { CWConnItem *item = [[CWConnItem alloc] initWithtype:cType url:sURL params:params Tag:0 key:nil blockWithOutTagAndKey:block blockWithOutKey:nil block:nil]; NSUInteger hashValue = [item hash]; [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]]; [item release]; return hashValue; } - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params tag:(NSInteger) nTag result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag)) block { CWConnItem *item = [[CWConnItem alloc] initWithtype:cType url:sURL params:params Tag:nTag key:nil blockWithOutTagAndKey:nil blockWithOutKey:block block:nil]; NSUInteger hashValue = [item hash]; [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]]; [item release]; return hashValue; } - (NSUInteger) conURL:(NSString *) sURL connectType:(connectType) cType params:(NSDictionary *) params tag:(NSInteger) nTag key:(NSString *) sKey result:(void(^)(BOOL bSuccess,id returnData,NSError *error,NSInteger nTag, NSString *skey)) block { CWConnItem *item = [[CWConnItem alloc] initWithtype:cType url:sURL params:params Tag:nTag key:sKey blockWithOutTagAndKey:nil blockWithOutKey:nil block:block]; NSUInteger hashValue = [item hash]; [self.connItems setObject:item forKey:[NSNumber numberWithUnsignedInteger:hashValue]]; [item release]; return hashValue; } #pragma mark - private action - (id) hasItem:(CWConnItem *) conn { NSUInteger hashValue = [conn hash]; id object = [self.connItems objectForKey:[NSNumber numberWithUnsignedInteger:hashValue]]; if (object == nil) { for(id item in [self.connItems allValues]) { if ([conn isEqual:item]) { return item; } } } else { return conn; } return nil; } - (void) removeItems:(CWConnItem *) conn { NSUInteger hashValue = [conn hash]; id object = [self.connItems objectForKey:[NSNumber numberWithUnsignedInteger:hashValue]]; if (object != nil) { [[(CWConnItem *)object httpReq] clearDelegatesAndCancel]; [self.connItems removeObjectForKey:[NSNumber numberWithUnsignedInteger:hashValue]]; } } - (void) didFinishedWithItems:(CWConnItem *) conn error:(NSError *) error { if (error == nil) { if (conn.delegate && [conn.delegate respondsToSelector:@selector(didCWFinishSuccessedWithData:tag:key:)]) { [conn.delegate didCWFinishSuccessedWithData:conn.httpReq.responseString tag:conn.nTag key:conn.sKey]; } } else { if (conn.delegate && [conn.delegate respondsToSelector:@selector(didCWFinishFailedWithError:tag:key:)]) { [conn.delegate didCWFinishFailedWithError:error tag:conn.nTag key:conn.sKey]; } } [self removeItems:conn]; } @end