NSUrlConnectionDelegate不调用方法来加载数据
我已经看过NSURLConnectionDelegate连接:didReceiveData不工作了,但似乎没有要与任何好的结果,所以我很好奇,为什么我不能够获得任何数据。
I have looked at NSURLConnectionDelegate connection:didReceiveData not working already, but there didn't seem to be any good result from that, so I am curious why I am not able to get any data.
我把断点 didReceiveResponse
和 didReceiveData code>。
它打印出连接成功,所以我知道该连接已启动。
It does print out "connection succeeded", so I know that the connection is started.
我使用ARC进行内存管理。
I am using ARC for memory management.
- (void)load {
request = [NSMutableURLRequest requestWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (conn) {
[conn start];
NSLog(@"connection succeeded, %s", [myURL description]);
responseData = [NSMutableData data];
} else {
NSLog(@"connection failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
更新:
要看看我如何测试这个看Asynchronous单元测试不被SenTestCase 调用。
To see how I test this look at Asynchronous unit test not being called by SenTestCase.
我没有落实jonkroll提到的两种方法,在他的回答,我只是没有告诉他们,但他们也没有被调用。
I did implement the two methods mentioned by jonkroll, in his answer, I just didn't show them, but, they also aren't being called.
我已经加入 [康恩开始]
不仅是因为它不工作,我希望可以解决这个问题,但没有这样的运气。
I had added [conn start]
only because it wasn't working, and I was hoping that may solve it, but no such luck.
在声明连接是这样的:
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
您正在创建一个本地指针。当你的方法完成,因为这是最后强烈参考 NSURLConnection的
, ARC
释放它。你需要使用一个强大的伊娃(和/或)财产持有强有力的参考 NSURLConnection的
创建。
You are creating a local pointer. When your method completes, since it was the last strong reference to the NSURLConnection
, ARC
releases it. You need to use a strong ivar (and/or) property to hold a strong reference to the NSURLConnection
you create.
修改
下面是我在一个示例项目测试code的基本样本。给它一个运行。详细日志记录帮助。
Here is basic sample of code that I tested in a sample project. Give it a run. Verbose logging helps.
@implementation <#Your class here#> {
// With ARC ivars are strong by default
NSMutableData *_downloadedData;
NSURLConnection *_connection;
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
NSHTTPURLResponse *realResponse = (NSHTTPURLResponse *)response;
if (realResponse.statusCode == 200){
// Really any 2** but for example
_downloadedData = [[NSMutableData alloc] init];
NSLog(@"Good response");
} else {
NSLog(@"Bad response = %i",realResponse.statusCode);
}
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
if (connection == _connection){
[_downloadedData appendData:data];
NSLog(@"Getting data...");
}
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection == _connection){
_connection = nil;
NSLog(@"We're done, inform the UI or the delegates");
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
_connection = nil;
NSLog(@"Oh no! Error:%@",error.localizedDescription);
}
- (void)load {
NSURL *url = [NSURL URLWithString:@"http://www.google.com/"];
NSURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalCacheData
timeoutInterval:60];
// Assign strong pointer to new connection
_connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
NSLog(@"Connection was initialized? = %@",(!!_connection)?@"YES":@"NO");
}
@end