2013-02-16 25 views
9

मैंNSURLConnection

[NSURLConnection connectionWithRequest:req delegate:self]; 

उपयोग कर रहा हूँ और फिर मैं

-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace; 
-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge; 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error; 
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response; 
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data; 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection; 

का उपयोग डेटा लोड हो रहा है संभाल करने के लिए। सब कुछ ठीक काम कर रहा है और ठीक है, लेकिन मैं

इस कोड की सुंदरता) मैं ब्लॉक का उपयोग करने, मेरे कोड इस तरह दिखता है करना चाहते हैं पसंद नहीं है:

[myConnection sendData:data 
      successBlock:^(void){NSLog(@"success");} 
      errorBlock:^(NSError * error){NSLog(@"error.description: %@", error.description);}]; 

यह संभव के साथ NSURLConnection उपयोग करने के लिए है ब्लॉक?

उत्तर

15

मैं इस वर्ग का उपयोग करें:

MyConnection.h

#import <Foundation/Foundation.h> 

@interface MyConnection : NSObject <NSURLConnectionDelegate, NSURLConnectionDataDelegate> { 
    NSURLConnection * internalConnection; 
    NSMutableData * container; 
} 

-(id)initWithRequest:(NSURLRequest *)req; 

@property (nonatomic,copy)NSURLConnection * internalConnection; 
@property (nonatomic,copy)NSURLRequest *request; 
@property (nonatomic,copy)void (^completitionBlock) (id obj, NSError * err); 


-(void)start; 

@end 

और MyConnection.m

#import "MyConnection.h" 

static NSMutableArray *sharedConnectionList = nil; 

@implementation MyConnection 
@synthesize request,completitionBlock,internalConnection; 

-(id)initWithRequest:(NSURLRequest *)req { 
    self = [super init]; 
    if (self) { 
     [self setRequest:req]; 
    } 
    return self; 
} 

-(void)start { 

    container = [[NSMutableData alloc]init]; 

    internalConnection = [[NSURLConnection alloc]initWithRequest:[self request] delegate:self startImmediately:YES]; 

    if(!sharedConnectionList) 
     sharedConnectionList = [[NSMutableArray alloc] init]; 
    [sharedConnectionList addObject:self]; 

} 


#pragma mark NSURLConnectionDelegate methods 

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { 

    [container appendData:data]; 

} 

//If finish, return the data and the error nil 
-(void)connectionDidFinishLoading:(NSURLConnection *)connection { 

    if([self completitionBlock]) 
     [self completitionBlock](container,nil); 

    [sharedConnectionList removeObject:self]; 

} 

//If fail, return nil and an error 
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { 

    if([self completitionBlock]) 
     [self completitionBlock](nil,error); 

    [sharedConnectionList removeObject:self]; 

} 

@end 

इसका इस्तेमाल करने की:

MyConnection * connection = [[MyConnection alloc]initWithRequest:req]; 
[connection setCompletitionBlock:^(id obj, NSError *err) { 

      if (!err) { 
       //It's ok, do domething with the response data (obj)     
      } else { 
       //There was an error 
      } 

     }]; 
[connection start]; 

यह बा है कोड पर sed, द बिग नेर्ड रंच अपनी पुस्तक पर उपयोग करता है।

+0

धन्यवाद jcesar, यह काम किया) बहुत बहुत धन्यवाद !! – Nils

+0

यह अजीब लगता है। यदि आप अपनी ऑब्जेक्ट पर एक और अनुरोध कहते हैं, तो समापन ब्लॉक ओवरराइट किया जा सकता है ... – Tudorizer

+0

सहायता के लिए धन्यवाद। ट्यूडरिज़र, आपको नए अनुरोध के लिए नया उदाहरण बनाना होगा। – Segabond

-1

मुझे आशा है कि यह सहायक होगा।

[NSURLConnection sendAsynchronousRequest:request 
            queue:[NSOperationQueue mainQueue] 
         completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) { 
          NSLog(@"%@", response); 
          NSLog(@"%@", data); 
         }]; 
+0

आप मुख्य कतार का उपयोग क्यों करते हैं? –

 संबंधित मुद्दे

  • कोई संबंधित समस्या नहीं^_^