iPhone NSURLConnection: connectionDidFinishLoading - wie man einen string an die aufrufende Methode

Habe ich überprüft, ähnlich wie stackoverflow-Fragen/Antworten in diesem, aber ich bin immer noch ratlos.

Ich bin ein Anfänger und ich bin wirklich mit diesen zu kämpfen. Mit dem iPhone kann ich den download von XML aus einer URL, aber ich kann nicht speichern der Ergebnis-string in einen NSString-Variablen von der aufrufenden Funktion.

Habe ich die folgenden Deklarationen in einer custom made-Klasse:

@interface comms : NSObject {
    NSString *currURL, *receivedString;
    NSMutableData *receivedData;
    NSURLConnection *conn;
}

@property (copy, readwrite) NSString *currURL;
@property (copy, readonly) NSString *receivedString;
@property (nonatomic, assign) NSMutableData *receivedData;
@property (nonatomic, assign) NSURLConnection *conn;

-(void) getContentURL;

Habe ich die folgende Methode in die comms-Klasse:

-(void) getContentURL
{
    NSLog( @"Begin getContentURL." );

    //Request data related.
    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] 
                                    autorelease];
    [request setURL:[NSURL URLWithString: currURL]];

    //Content-Type related.
    [request setValue:@"application/x-www-form-urlencoded" 
   forHTTPHeaderField:@"Content-Type"];

    //Create Connection.
    conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (conn) {
        //The connection was established.
        receivedData = [[NSMutableData data] retain];
        NSLog( @"Data will be received from URL: %@", request.URL );
    }
    else
    {
        //The download could not be made.
        NSLog( @"Data could not be received from: %@", request.URL );
    }

    //PROBLEM - receivedString is NULL here.
    NSLog( @"From getContentURL: %@", receivedString );
}

Habe ich erstellt die erforderlichen Delegierten in den comms-Klasse gemäß der folgenden:

-(void)connection:(NSURLConnection *)connection didReceiveResponse:
    (NSURLResponse *)response
{
    //Discard all previously received data.
    [receivedData setLength:0];
}

-(void)connection:(NSURLConnection *)connection didReceiveData:
(NSData *)data
{
    //Append the new data to the receivedData.
[receivedData appendData:data];     
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    //Connection succeeded in downloading the request.
    NSLog( @"Succeeded! Received %d bytes of data", [receivedData length] );

    //Convert received data into string.
    receivedString = [[NSString alloc] initWithData:receivedData 
        encoding:NSASCIIStringEncoding];
    NSLog( @"From connectionDidFinishLoading: %@", receivedString );

    //release the connection, and the data object
    [conn release];
    [receivedData release];
}

Kann ich erfolgreich die Ausgabe der receivedString string mit NSLog in die connectionDidFinishLoading delegieren.

    //Convert received data into string.
    receivedString = [[NSString alloc] initWithData:receivedData 
        encoding:NSASCIIStringEncoding];
    NSLog( @"From connectionDidFinishLoading: %@", receivedString );

Allerdings, wenn ich die Ausgabe der receivedString string in der getContentURL es null (und damit auch null aus dem ViewController.m-Klasse, die ich rufen Sie die comms Klasse aus).

    //PROBLEM - receivedString is NULL here.
    NSLog( @"From getContentURL: %@", receivedString );

Irgendwelche Ideen auf, wie kann ich den Wert von receivedString in getContentURL und von dem ViewController.in der m-Klasse?

InformationsquelleAutor | 2010-02-12

Schreibe einen Kommentar