9

使っています

[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を使用することは可能ですか?

4

2 に答える 2

16

私はこのクラスを使用します:

The 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];

これは、The BigNerdRanchが彼の本で使用しているコードに基づいています。

于 2013-02-16T20:45:24.243 に答える
-1

お役に立てば幸いです。

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
                           NSLog(@"%@", response);
                           NSLog(@"%@", data);
                       }];
于 2016-06-22T07:57:31.700 に答える