nsurlconnectiondelegate并未被称为



我是iOS世界的另一个新手,并且一直在试图弄清Nsurlconnection如何与其代表一起使用。我没有很多运气。在网上查看了几个示例之后,我创建了以下测试类。我遇到的问题是,我的代表方法都没有被调用。我的超时循环存在每个显示的无跟踪消息。我已经通过TCPMOM运行了此操作,可以看到请求出去并发送回响应,但是没有任何东西将其交付给我的代表。

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

谢谢。

这是我的代码:

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

您需要将连接委托设置为nsurlConnectionDelegate类型的,而不是标题中的NsurlConnectionDataDelegate。据我所知。

@interface TestRequester : NSObject <NSURLConnectionDelegate>

请参阅此页面上的连接协议下的零件:)https://develvemer.apple.com/library/mac/#documentation/cocoa/reference/foundation/foundation/classes/nsurlconnection_class/reference/reference/reference/reference.html#//pac_reff/doc/c_ref/c_ref/c_ref/nsurlconnection.

看起来您的连接范围不在范围中。您可能正在使用弧。

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!");
}
}

最新更新