IPhone NSURLConnection: connectionDidFinishLoading - how to return a string to the calling method

I looked at similar questions and answers on stackoverflow, but I'm still at a dead end.

I am a newbie and I am really struggling with this. From iPhone, I can load XML from the URL, but I cannot save the result string in the NSString variable and see it from the calling function.

I have the following declarations in a custom class:

@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; 

I have the following method in the comms class:

 -(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 ); } 

I created the necessary delegates in the comms class as follows:

 -(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]; } 

I can successfully output the receivedString string using NSLog in the connectionDidFinishLoading delegate.

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

However, when I exit the receivedString line in getContentURL, this is null (and therefore also null from the ViewController.m class, from which I call the comms class).

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

Any ideas on how I can see the value of the resulting string in getContentURL and the ViewController.m class?

+4
source share
2 answers

NSURLConnection is an asynchronous API. When you start the request, the object will spawn a new thread and update your main one only using callback / delegation methods. Your current method will return, most likely, before the request is completed, and therefore the result line will not be loaded!

If you want to do this synchronously, you will have two options:

  • Use the built-in synchronous boot method. Please note: since this is blocking, it will not allow the user to interact with the user interface.
  • Use the C functions CFRunLoopRun () and CFRunLoopStop () to start a run loop inside your calling function, wait for the load to complete or fail, and then return control to the calling method using CFRunLoopStop ().
+4
source

To return data from the delegate method, use a code block.

See Apple Blocks Documentation .

0
source

Source: https://habr.com/ru/post/1301076/


All Articles