iphone - NSURLConnection 委托(delegate)方法没有被调用

标签 iphone objective-c ios macos asynchronous

我遇到了这个问题,我在堆栈溢出和其他地方浏览了很多帖子,但找不到解决方案。我也在主线程上运行它。代码如下。

@interface JsonViewController : UIViewController <UIActionSheetDelegate,UIWebViewDelegate,NSURLConnectionDelegate>
{
  ....
}
@implementation JsonViewController
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://google.com"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:60];
    rssFeedDetailViewConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    NSLog(@"PRINTING RSS FEED %@", rssFeedDetailViewConnection);
[rssFeedDetailViewConnection scheduleInRunLoop:[NSRunLoop mainRunLoop] 
                                       forMode:NSDefaultRunLoopMode];
    [rssFeedDetailViewConnection start];
}


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
NSLog(@"Hello");
responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
     NSLog(@"Hello");
    [responseData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
     NSLog(@"Hello");
    [responseData release];
    [connection release];
// Show error message
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
 NSLog(@"Hello");
    // Use responseData
    [responseData release];
    [connection release];
 }

任何帮助将不胜感激。我已经被这个问题困扰了两天了..

最佳答案

您不需要这些语句:

[rssFeedDetailViewConnection scheduleInRunLoop:[NSRunLoop mainRunLoop]  forMode:NSDefaultRunLoopMode];
[rssFeedDetailViewConnection start];

因为您正在使用initWithRequest:delegate:。此调用已经开始加载数据。

如果删除这些特定语句会发生什么?

有关 NSURLConnection 的更多信息.

关于iphone - NSURLConnection 委托(delegate)方法没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12053330/

相关文章:

iphone - 向下移动 UITabBarItem 图像?

ios - 获取相册列表的简单示例?

ios - 针对不同分辨率设备的页面背景图像缩放

ios - 如何在过渡中使 ECSlidingViewController View Controller 变暗?

iphone - 为什么保留/释放而不是新建/删除?

iphone - 如何在应用内购买中恢复购买?

iphone - Xcode 断点不显示当前对象

ios - Swift3 上的 "Empty collection literal requires an explicit type"错误

ios - 如何更改 UISearchBar 占位符和图像色调颜色?

iphone - 如何获得 UITableView 的可见矩形?