NSURLConnectionはdidRecieveDataメソッドを呼び出しません
-
06-07-2019 - |
質問
iPhone SDKドキュメントで、インターネットからデータをダウンロードするアプリケーションを作成したい ダウンロードに使用されるNSURLConnectionクラスを見つけました。 ドキュメントにあるのと同じコードを書いて実行しました。接続は正常に作成されましたが、データはダウンロードされませんでした。 connectionDidFinishLoadingは、1〜2秒後に起動されますが、結果のデータはありません。問題は、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];
}
解決
" receive"のつづりを間違えました:
// Your signature
- (void)connection:(NSURLConnection *)connection didRecieveData:(NSData *)data;
// Correct signature
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;
他のヒント
didReceiveDataメソッドの名前にタイプミスがあります(iはeの前、cの後は除きます:-)
したがって、クラスはその(オプションの)セレクターを実装していないように見え、黙って無視されます。
所属していません StackOverflow