Ich möchte meine Anwendung einige Daten aus dem Internet herunterladen, in iPhone SDK Dokumentation Ich fand NSURLConnection Klasse, die zum Herunterladen verwendet wird, Bin ich richtig? Ich habe den gleichen Code wie in der Dokumentation geschrieben und ausgeführt. Verbindung wurde erfolgreich erstellt, aber keine Daten wurden heruntergeladen. connectionDidFinishLoading wird nach sec oder zwei aber ohne Daten im Ergebnis ausgelöst. Problem ist, dass die Methode didRecieveData nie ausgelöst wurde. Ich weiß nicht warum, ich suchte im Internet, aber jedes Ergebnis war der gleiche Code wie in der Dokumentation. Könnten Sie bitte einen Rat geben? Danke für jede Antwort Mein Downloader-Klassen-Quellcode ist unten.NSURLConnection ruft nicht didRecieveData-Methode
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];
}