NSURLConnectionDelegate未被调用

时间:2012-11-12 22:26:33

标签: iphone ios nsurlconnection nsurlconnectiondelegate

我是iOS世界的另一个新手,并且一直试图弄清楚NSURLConnection如何与它的委托一起工作。我运气不好。在网上查看了几个例子之后,我创建了以下测试类。我遇到的问题是我的委托方法都没有被调用过。我的超时循环存在,每次显示都没有跟踪消息。我通过tcpmon运行它,可以看到请求发出并且回复了一个响应,但没有任何内容传递给我的代表。

任何人都可以告诉我我做错了什么。

感谢。

这是我的代码:

TestRequest.h

#import <Foundation/Foundation.h>

@interface TestRequester : NSObject <NSURLConnectionDataDelegate>

@property BOOL finished;

-(void)testRequest;
@end

实施:

#import "TestRequester.h"

@implementation TestRequester

-(void)testRequest {

    NSString *url = @"<your favourite URL>";
    NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                                cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                                timeoutInterval:60.0];
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
    if (theConnection) {
        NSLog(@"Connection created.  Waiting....");
        int count = 20;
        while (!_finished && count-- > 0)
            [NSThread sleepForTimeInterval:0.5];
        if (_finished)
            NSLog(@"Request completed");
        else
            NSLog(@"Request did not complete");
    } else {
        NSLog(@"Connection was not created!");
    }

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    NSLog(@"-------------> Received data");
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    NSLog(@"-------------> Received response");
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"-------------> connectionDidFinishLoading");
    _finished = YES;
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"-------------> Received error");
    _finished = YES;
}

@end

2 个答案:

答案 0 :(得分:0)

您需要在页眉中将连接委托设置为NSURLConnectionDelegate类型而不是NSURLConnectionDataDelegate。据我所见。

@interface TestRequester : NSObject <NSURLConnectionDelegate>

请参阅本页连接协议下的部分:) https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLConnection_Class/Reference/Reference.html#//apple_ref/doc/c_ref/NSURLConnection

答案 1 :(得分:0)

看起来您的连接超出了范围。你可能正在使用ARC。

TestRequest.h

#import <Foundation/Foundation.h>

@interface TestRequester : NSObject <NSURLConnectionDelegate>
{
    NSURLConnection *theConnection;
}
@property BOOL finished;

-(void)testRequest;
@end

-(void)testRequest {

NSString *url = @"<your favourite URL>";
NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]
                            cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
                            timeoutInterval:60.0];
theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self startImmediately:YES];
if (theConnection) {
    NSLog(@"Connection created.  Waiting....");
    /*int count = 20;
    while (!_finished && count-- > 0)
        [NSThread sleepForTimeInterval:0.5];
    if (_finished)
        NSLog(@"Request completed");
    else
        NSLog(@"Request did not complete");*/
} else {
    NSLog(@"Connection was not created!");
}

}