gpt4 book ai didi

iphone - NSURLConnection 不调用 didRecieveData 方法

转载 作者:行者123 更新时间:2023-12-03 21:00:20 25 4
gpt4 key购买 nike

我希望我的应用程序从 iPhone SDK 文档中的互联网下载一些数据我找到了 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];
}

最佳答案

您拼错了“接收”:

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

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

关于iphone - NSURLConnection 不调用 didRecieveData 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1026780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com