【问题标题】:NSURLConnection doesn't call didRecieveData methodNSURLConnection 不调用 didRecieveData 方法
【发布时间】:2009-06-22 11:56:45
【问题描述】:

我希望我的应用程序在 iPhone SDK 文档中从 Internet 下载一些数据 我找到了用于下载的 NSURLConnection 类,对吗? 我编写了与文档中相同的代码并运行了它。连接已成功创建,但未下载任何数据。 connectionDidFinishLoading 在一两秒后被触发,但结果中没有数据。问题是, didRecieveData 方法永远不会被触发。我不知道为什么,我搜索了互联网,但每个结果都与文档中的代码相同。你能给个建议吗?感谢您的每一个回复 我的下载器类源代码如下。

Downloader.h

@interface Downloader : NSObject {
    NSURLConnection *conn;

    //Array to hold recieved data
    NSMutableData *recievedData;
}

@property (nonatomic, retain) NSURLConnection *conn;
@property (nonatomic, retain) NSMutableData *recievedData;

- (void)downloadContentsOfUrl:(NSURL *)url;

@end

Downloader.m

#import "Downloader.h"
@implementation Downloader
@synthesize recievedData, conn;

- (void)connection:(NSURLConnection *)connection didRecieveResponse:(NSURLResponse *)response
{
    NSLog(@"did recieve response");

    [recievedData release];
    recievedData = nil;
}

- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)data
{
    NSLog(@"did recieve data");
    //Append the new data to the recieved data
    [recievedData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    //Release the connection and the data object
    [connection release];
    [recievedData release];

    NSLog(@"Connection failed! Error - %@ %@", [error localizedDescription],
          [[error userInfo] objectForKey:NSErrorFailingURLStringKey]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //ToDo with data
    //[recievedData writeToFile:@"data" atomically:YES];
    NSLog(@"downloaded");
    NSLog(@"%u", [recievedData length]);
    //Release the connection and the data object
    [connection release];
    [recievedData release];
}

- (void)downloadContentsOfUrl:(NSURL *)url
{
    //Create the connection
    //Create the request
    NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url 
            cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];

        //Create the connection with the request and start loading the data
    conn =  [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self 
                startImmediately:YES];
    if(conn)
    {
        //Create the NSMutableData that will hold the recieve data
        recievedData = [[NSMutableData data] retain];
        NSLog(@"Connection success!");
    }
    else
    {
        NSLog(@"Can't download this file!");
    }       
}

- (void)dealloc
{
    [conn release];
    [recievedData release];

    [super dealloc];
}

【问题讨论】:

    标签: iphone nsurlconnection download


    【解决方案1】:

    你拼错了“receive”:

    // Your signature
    - (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)data;
    
    // Correct signature
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
    

    【讨论】:

      【解决方案2】:

      您的 didReceiveData 方法的名称中有错字(i 在 e 之前,c 之后除外 :-)

      因此,您的类看起来好像没有实现该(可选)选择器,它将被静默忽略。

      【讨论】:

        猜你喜欢
        • 2011-08-12
        • 1970-01-01
        • 2012-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-07-03
        相关资源
        最近更新 更多