NSURLConnection的委托方法不会被调用(NSURLConnection delegate

2019-09-18 08:12发布

我试图使用NSURLConnection的委托方法。 下面的方法目前不被调用:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

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

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
//I also want to be able to use self-signed https urls

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace;
//I also want to be able to use self-signed https urls

我目前使用同步调用,而是因为在我完成的代码库我要落实到一个iPhone应用程序,我不能让我的UI冻结异步似乎更好。

用下面的方法responseData = [NSMutableData dataWithData:[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]];

我回到我所需要的数据,但使用异步我看来,我必须使用委托方法来找回数据。 我试图通过使用以添加代理@interface myClass : NSObject<NSURLConnectionDelegate>

我打电话给我的方法如下:

-(void)grabData{
    NSArray* array = [NSArray arrayWithObjects:@"auth.login",@"user",@"pass", nil];
    NSData* packed_array = [array messagePack];

    NSURL* url = [NSURL URLWithString:@"https://192.168.1.115:3790/"];
    NSMutableURLRequest* request = [[[NSMutableURLRequest alloc]initWithURL:url]retain];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"RPC Server" forHTTPHeaderField:@"Host"];
    [request setValue:@"binary/message-pack" forHTTPHeaderField:@"Content-Type"];
    [request setValue:[NSString stringWithFormat:@"%d",[packed_array length]] forHTTPHeaderField:@"Content-Length"];
    [request setHTTPBody:packed_array];

    //NSHTTPURLResponse *response = nil;
    NSError *error = nil;

    NSLog(@"connecting");
    NSURLConnection* connection = [[[NSURLConnection alloc]initWithRequest:request delegate:self]retain];
    if (connection) {
        NSLog(@"connection exists");
        self.responseData = [[NSMutableData data]retain];
    }
    else {
        NSLog(@"Connection doesn't exist?");
    }
    NSLog(@"response data: %@",[responseData messagePackParse]);
    NSLog(@"error: %@",error);
}

我已经尝试了以下情况:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];

Answer 1:

我继续寻找相关的问题,并找到这个答案最有帮助的 。 这使我使用下面的方法。 首先,我宣布在叫我的界面完成一个BOOL属性,并以我的实现造成被称为我的委托方法添加下面的方法。

while(!finished) {
    [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}


Answer 2:

只是以为 - 你正在使用自动引用计数,并考虑创建NSURLConnection的调用[连接开始]当委托方法应该叫(即数据已经被接收)的时间之间被释放对象的任何机会呢?

做任何事情,以保持您所使用创建NSURLConnection的对象的引用?

根据附加信息,它似乎是,这是一个可能的原因。 您可能遇到的代码,如应用程序委托以下(或你已经创建另一个类)。

MyClass *object = [[MyClass alloc] init];
[object grabData];

这是great-它实例化一个对象,然后告诉对象抢有问题的数据。 但是,一旦grabData完成(和NSURLConnection的返回数据之前),对象将因为没有什么是坚持到它被释放。 为了解决这一问题:

  1. 创建您的.h文件中的属性(如创建对象)守住你的MyClass实例。

    @属性(强)MyClass的*对象;

  2. 合成该财产在.m文件

    @synthesize对象;

  3. 而不仅仅是创建对象,持有对它的引用:

    MyClass的* MyClass的= [[MyClass的页头] INIT];

    [MyClass的grabData];

    self.object = MyClass的;

这将防止您的对象由ARC被释放。

当你完全这个对象(您NSURLConnection的返回数据),可以设置self.object =零,以摆脱参考。



Answer 3:

您可以使用此:

NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest: request delegate:self startImmediately:NO];

[connection scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];

[connection start];


Answer 4:

如果你遇到麻烦,你可以做什么,我在过去所做的那样,用像这样同步请求:

NSHTTPURLResponse *response = nil;
NSError *error = nil;
[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]

这将输出你需要的数据,所有你需要做它不紧你的UI是使用块(GCD):

dispatch_queue_t downloadQueue = dispatch_queue_create("download queue", NULL);
dispatch_async(downloadQueue, ^{
  NSHTTPURLResponse *response = nil;
  NSError *error = nil;
  NSData *myData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]
  dispatch_async(dispatch_get_main_queue(), ^{
    // Handle your response data
  });
});


Answer 5:

建立连接后只写在下面

[连接开始]; 您的委托方法将被调用。



文章来源: NSURLConnection delegate methods not being called