NSURLConnection委托方法不执行 [英] NSURLConnection delegate methods not executing

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

问题描述

我正在运行Apple的以下示例代码 -

I'm running the following example code from Apple -

        NSString *requestURL = [[NSString alloc] initWithString:@"http://google.com/"];         
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:requestURL] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
        NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

        if(theConnection) {

            NSLog(@"The connection has started...");

        } else {

            NSLog(@"Connection has failed...");

        }

但下面的委托方法没有被调用。有人可以帮帮我吗?

But the delegate methods below aren't being called. Can anyone please help me out?

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {


推荐答案

如果您在后台线程上执行此操作,则可以在调用委托之前退出该线程。在调用触发后台线程CFRunLoopRun()的方法之后,查看调用刚开始传输的方法。然后在你的回调中 connectionDidFinishLoading: connection:didFailWithError:确保你也运行:

If you are performing this on a background thread, the thread is probably exiting before the delegates can be called. Look at calling in your method that starts the transfer just after you call the method that fires off the background thread, CFRunLoopRun(). Then in your callbacks connectionDidFinishLoading: and connection:didFailWithError: make sure you also run:

CFRunLoopStop(CFRunLoopGetCurrent());

在这些方法结束时,否则你永远不会返回。

At the end of those methods, otherwise you will never return.

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

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