Ich bin ziemlich neu in iOS-Entwicklung und ich versuche, Inhalt von einer URL zu erhalten. Ich benutze das Apple-Tutorial für die Verwendung von NSURLConnection, alles funktioniert, aber ich bekomme keine Daten. Ich habe mich umgesehen und konnte keine Antwort auf mein Problem finden. Ich verwende ARC auch in meinem Projekt, vielleicht verursacht das das Problem? HierNSURLConnection mit ARC empfangen keine Daten
ist der Code, ich verwende, beginnend mit meiner Header-Datei:
@interface ViewController : UIViewController
@property (nonatomic, retain) NSMutableData *receivedData;
@end
Implementierungsdatei:
@implementation ViewController
@synthesize receivedData;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidLoad
{
[super viewDidLoad];
NSLog(@"1. viewDidLoad");
// Create the request
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.apple.com/"] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
// Create the connection with the request and start loading the data
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (connection) {
NSLog(@"2. connection succeeded");
receivedData = [NSMutableData data];
} else {
NSLog(@"2. connection failed");
}
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"3. didReceiveResponse");
[receivedData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[receivedData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"4. Connection failed! Error - %@ %@", [error localizedDescription], [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"4. Succeeded! Received %d bytes of data", [receivedData length]);
}
Alle NSLog arbeiten, aber es sagt immer receivedData hat 0 Byte . Ich konnte keine Antwort auf mein Problem finden, also hoffe ich, dass ich meine Antwort mit dieser Frage finden kann.
ARC sollte nicht für automatische Referenzzählung verwendet werden, bitte lesen Sie das Tag-Wiki. –
Es tut mir leid, dass –
Lars, für ARC-Code, Ihre '@ Eigenschaft' sollte 'strong' anstelle von' retain' verwenden. Warum haben Sie auch keine Log-Anweisung in Ihrer '-connection: didReceiveData:' Methode? Andrew – adonoho