作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我希望我的应用程序从 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/
我希望我的应用程序从 iPhone SDK 文档中的互联网下载一些数据我找到了 NSURLConnection 类,它是用于下载的,对吗?我编写了与文档中相同的代码并运行了它。连接已成功创建,但未下载
我是一名优秀的程序员,十分优秀!