NSURLConnection委托方法未被调用 [英] NSURLConnection delegate methods not being called

查看:95
本文介绍了NSURLConnection委托方法未被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用NSURLConnection的委托方法。
目前尚未调用以下方法:

I'm trying to use the delegate methods of NSURLConnection. The following methods are currently not being called:

- (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冻结。

I'm currently using a synchronous call but asynchronous seems better because after I complete the code base I'm going to implement it into an iPhone application and I can't have my ui freezing.

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

我得到了我需要的数据但是使用异步我似乎必须使用委托方法来获取数据。我尝试使用 @interface myClass添加委托:NSObject< NSURLConnectionDelegate>

I get back the data I need but using asynchronous I seems I have to use the delegate methods to get data back. I tried to add the delegate by using @interface myClass : NSObject<NSURLConnectionDelegate>

我正在调用我的方法如下:

I'm calling my method as follows:

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


推荐答案

我继续搜索相关问题并找到这个答案最有帮助。它引导我使用以下方法。
首先,我在我的界面中声明了一个名为finished的BOOL属性,并在我的实现中添加了以下方法,这导致我的委托方法被调用。

I continued to search for related questions and found this answer most helpful. It lead me to using the following method. First I declared a BOOL property called finished in my interface and added the following method in my implementation which caused my delegate methods to be called.

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

这篇关于NSURLConnection委托方法未被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆