Three20 NetWork

16612 단어 NetWork
Three20 NetWork  NSUrlConnection 을 위 한 강력 한 네트워크 처리 모듈 로 강력 한 캐 시 메커니즘 을 실현 했다.
1. 우선 우리 먼저 보 자 TTURLRequest 같은 종 류 는 http method, body 를 사용자 정의 할 수 있 습 니 다. parameters, as well as natural response.  processing using TTURLResponse objects. 
#import <Foundation/Foundation.h>

// Network

#import "Three20Network/TTURLRequestCachePolicy.h"

// Core

#import "Three20Core/TTCorePreprocessorMacros.h"// For __TTDEPRECATED_METHOD

@protocol TTURLRequestDelegate;

@protocol TTURLResponse;

@interface TTURLRequest : NSObject {

  NSString*             _urlPath;             //url  

  NSString*             _httpMethod;     //http  

  NSData*               _httpBody;          //http 

  NSMutableDictionary*  _parameters;  //  

  NSMutableDictionary*  _headers;       //http 

  NSString*             _contentType;           //    

  NSStringEncoding      _charsetForMultipart;  //     

  NSMutableArray*       _files;                     //      

  id<TTURLResponse>     _response;       

  TTURLRequestCachePolicy _cachePolicy;         //    

  NSTimeInterval          _cacheExpirationAge;        //      ,   1 

  NSString*               _cacheKey;                               //  key

  NSDate*               _timestamp;                                

  NSInteger             _totalBytesLoaded;               //              

  NSInteger             _totalBytesExpected;           //        

  NSInteger             _totalBytesDownloaded;      //       

  NSInteger             _totalContentLength;         //      

  id    _userInfo;                                                 //    

  BOOL  _isLoading;                                         //      

  BOOL  _shouldHandleCookies;                   //    cooke

  BOOL  _respondedFromCache;                    //        

  BOOL  _filterPasswordLogging;                  //      

  NSMutableArray* _delegates; 

}

/**

 *      url  

 */

@property (nonatomic, copy) NSString* urlPath;

/**

   ,       , urlPath  

 */

@property (nonatomic, copy) NSString* URL__TTDEPRECATED_METHOD;

/**

 *    http     

 * @example @"POST"

 * @example @"GET"

 * @example @"PUT"

 * @   (t @"GET")

 */

@property (nonatomic, copy) NSString* httpMethod;

/* *     response data             ,           ,   TTURLXMLResponse           extThree20XML

 * @see TTURLDataResponse

 * @see TTURLImageResponse

 * @see TTURLXMLResponse

 */

@property (nonatomic, retain) id<TTURLResponse> response;

/**

 *       Http  body

 *      ,      .      POST put      ,    。 

 *    httpBody   ,  post put  data   parameters       ,     

 */

@property (nonatomic, retain) NSData* httpBody;

/**

 *           The content type of the data in the request.

 *       ,  httpMethod post  put ,  contentType  @"multipart/form-data".

 */

@property (nonatomic, copy) NSString* contentType;

/**

 *  HTTP POST/PUT     

 */

@property (nonatomic, readonly) NSMutableDictionary* parameters;

/**

 *    HTTP headers.

 */

@property (nonatomic, readonly) NSMutableDictionary* headers;

/**

 * @    : TTURLRequestCachePolicyDefault

 */

@property (nonatomic)TTURLRequestCachePolicy cachePolicy;

/**

 *    response         

 *   TT_DEFAULT_CACHE_EXPIRATION_AGE (1 week)

 */

@property (nonatomic)NSTimeInterval cacheExpirationAge;

/**

 *     cache key,      key  request data   。

 *     post put,  post put      cache key   。

 *     cache key ,        cache key,      。

 */

@property (nonatomic, copy) NSString* cacheKey;

/**

 *        ,             

 *  TTUserInfo        

 * @see TTUserInfo

 */

@property (nonatomic, retain) id userInfo;

@property (nonatomic, retain) NSDate* timestamp;

/**

 *          

 */

@property (nonatomic) BOOL isLoading;

/**

 *          cookie  

 * @param YES if cookies should be sent with and set for this request;

 *        otherwise NO.

 * @discussion The default is YES -   cookies are sent from and

 *             stored to the cookie manager by default.

 * @default YES

 */

@property (nonatomic)BOOL shouldHandleCookies;

/**

 *           

 */

@property (nonatomic)NSInteger totalBytesLoaded;

/**

 *            

 */

@property (nonatomic)NSInteger totalBytesExpected;

/**

 *              

 */

@property (nonatomic)NSInteger totalBytesDownloaded;

/**

 *          

 */

@property (nonatomic)NSInteger totalContentLength;

/**

 *        

 *            

 */

@property (nonatomic)BOOL respondedFromCache;

/**

               .

 */

@property (nonatomic,assign) BOOL filterPasswordLogging;

/**

 *    multipart/form-data           *

 * @    NSUTF8StringEncoding 

 */

@property (nonatomic)NSStringEncoding charsetForMultipart;

/**

 * An array of non-retained objects that receive messages about the progress of the request.

 */

@property (nonatomic, readonly) NSMutableArray* delegates;

+ (TTURLRequest*)request;

+ (TTURLRequest*)requestWithURL:(NSString*)URL delegate:(id/*<TTURLRequestDelegate>*/)delegate;

- (id)initWithURL:(NSString*)URL delegate:(id/*<TTURLRequestDelegate>*/)delegate;

- (void)setValue:(NSString *)value forHTTPHeaderField:(NSString *)field;

/**

 *      ,          

 */

- (void)addFile:(NSData*)data mimeType:(NSString*)mimeType fileName:(NSString*)fileName;

/**

 *         

 *             ,      

 *             。

 * @return YES             ,      

 */

- (BOOL)send;

/**

 *           

 *               ,        

 */

- (BOOL)sendSynchronously;

/**

 *     

 *        url  ,       

 */

- (void)cancel;

- (NSURLRequest*)createNSURLRequest;

@end

 
TTURLRequest 대상 만 들 기 
TTURLRequest *request  = [TTURLRequestrequestWithURL:kRequestURLPath delegate:self];

TTURLRequestDelegate 프로 토 콜 실현
그림 처리 요청 사용: TTURLImage Response, TTURLImage Response 는 response types 중의 하나 일 뿐 사용 할 수 있 습 니 다. TTURLDataResponse 와 TTURLXMLResponse (이 xmlResponse 는 확장 프레임 워 크 를 추가 해 야 합 니 다 extThree20XML )
request.response = [[[TTURLImageResponsealloc] init]autorelease];



[request send];  //       

 
#pragma mark -



#pragma mark TTURLRequestDelegate



//    



- (void)requestDidStartLoad:(TTURLRequest*)request {



  [_requestButtonsetTitle:@"Loading..."forState:UIControlStateNormal];



}



//    



- (void)requestDidFinishLoad:(TTURLRequest*)request {



  TTURLImageResponse* imageResponse = (TTURLImageResponse*)request.response;



  if (nil == _imageView) {



    _imageView = [[UIImageView alloc] initWithFrame:CGRectZero];



    [_scrollView addSubview:_imageView];



  }



  _imageView.image = imageResponse.image;



  [_imageView sizeToFit];



  _imageView.alpha = 0;



  [UIView beginAnimations:nil context:nil];



  [UIView setAnimationDuration:0.5];



  _requestButton.alpha = 0;



  _clearCacheButton.alpha = 0;



  [_scrollView setContentSize:_imageView.frame.size];



  _imageView.alpha = 1;



  [UIView commitAnimations];



}



//      



- (void)request:(TTURLRequest*)request didFailLoadWithError:(NSError*)error {



  [_requestButtonsetTitle:@"Failed to load, try again."forState:UIControlStateNormal];



}

좋은 웹페이지 즐겨찾기