【问题标题】:NSURLConnectionDelegate is not being calledNSURLConnection Delegate 未被调用
【发布时间】:2012-11-01 09:37:43
【问题描述】:

我是 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

【问题讨论】:

  • 请显示调用这个类的代码。

标签: iphone ios nsurlconnection nsurlconnectiondelegate


【解决方案1】:

您需要在标题中将连接委托设置为 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

【讨论】:

  • 无论我使用 NSURLConnectionDelegate 还是 NSURLConnectionDataDelegate,我都会得到相同的结果。
【解决方案2】:

您的连接似乎超出了范围。您可能正在使用 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!");
}

}

【讨论】:

  • 连接如何超出范围?连接是在 testRequest 方法中创建的,直到 "while" 循环完成后才会退出。
  • 无论如何,按照您的方式进行操作不会改变结果。
  • 我第一次看的时候错过了那个循环。摆脱那个循环,它阻塞了线程。我已经编辑了代码。
  • 使用 Arc 可以防止这种情况发生吗?多么糟糕的解决方案! Arc 是每个人都应该并且正在使用的东西。 . .
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-20
  • 2011-07-01
  • 2011-10-18
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
相关资源
最近更新 更多