Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7077661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:27:04+00:00 2026-05-28T06:27:04+00:00

This might be a dumb question. Sorry if it is. But Im working on

  • 0

This might be a dumb question. Sorry if it is.

But Im working on a project that consumes web services. I can connect to the web service and get the data I need fine.

I would like to have a method that returns this data obtained from the web service to the caller. The only problem is that the data is only obtained inside the ConnectionDidFinishLoading method, and I can’t access this data from my method.

here is my code, that works fine:

- (NSData *) dataForMethod:(NSString *)webMethod withPostString:(NSString *)postString
{
    NSURL *url = [NSURL URLWithString:[SigameWebServiceAddress stringByAppendingFormat:@"%@%@", @"/", webMethod]];
    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];

    [req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [req addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody: [postString dataUsingEncoding:NSUTF8StringEncoding]];

    conn = [[NSURLConnection alloc] initWithRequest:req delegate:self];
    if (conn) 
    {
        webData = [NSMutableData data];
    }   

    // I WOULD LIKE TO RETURN WEBDATA TO THE CALLER HERE, BUT WEBDATA IS EMPTY NOW, THE  
    //connectionDidFinishLoading ONLY GETS CALLED WITH THE DATA I WANT AFTER THE COMPILER
    //IS DONE EXECUTING MY METHOD.
}

-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *) response 
{
    [webData setLength: 0];
}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data 
{
    [webData appendData:data];
}

-(void) connection:(NSURLConnection *) connection didFailWithError:(NSError *) error 
{
    NSLog(@"FATAL ERROR");
}

-(void) connectionDidFinishLoading:(NSURLConnection *) connection 
{
    NSLog(@"DONE. Received Bytes: %d", [webData length]);

    NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];

    //---shows the XML---
    NSLog(@"%@", theXML);  //NOW, THIS IS THE DATA I WANT. BUT HOW CAN I RETURN THIS TO 
                           //THE CALLER. I MEAN, THE CALLER THAT CALLED MY METHOD 
                           //+ (NSData *) dataForMethod: withPostString:
}

Any help here is appreciated!
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T06:27:05+00:00Added an answer on May 28, 2026 at 6:27 am

    There are really two ways to go about this.

    1. Create a delegate interface
    2. Use Blocks

    I would strongly advise against using the synchronous methods – unless you are/have created your own asynchronous framework around them (i.e. you are manually starting another thread and executing your synchronous request on that thread). In the long run you will realize you need the requests to be async, and you’ll have to re-work everything such that they are.

    To give a quick overview of the two options I gave:

    1. Create a delegate interface

    The idea here is to create a class which performs the request, and create a protocol the caller must implement. When the request is complete, you will invoke a specified method on the delegate with the data:

    The protocol might look something like this:

    @protocol RequestClassDelegate <NSObject>
    
    - (void)requestCompleted:(ResponseClass *)data;
    - (void)requestError:(NSError *)error;
    
    @end
    

    The class which makes the request might look something like this:

    @interface RequestClass : NSObject
    
    - (void)makeRequest:(id<RequestClassDelegate>)delegate;
    
    @end
    

    And the request class implementation might contain some of the following, in addition to your connection logic:

    @implementation RequestClass
    {
        __weak id<RequestClassDelegate> _delegate;
    }
    
    // Connection Logic, etc.
    
    - (void)makeRequest:(id<RequestClassDelegate>)delegate
    {
        _delegate = delegate;
        // Initiate the request...
    }
    
    -(void) connectionDidFinishLoading:(NSURLConnection *) connection 
    {
        NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    
        // Processing, etc.
    
        // Here we'll call the delegate with the result:
        [_delegate requestCompleted:theResult];
    }
    
    @end
    

    2. Use Blocks

    This solution is much the same as the first solution – but, a bit more elegant in my opinion. Here, we’ll change the RequestClass to use blocks instead of a delegate:

    typedef void (^requestCompletedBlock)(id);
    typedef void (^requestErrorBlock)(NSError *);
    @interface RequestClass : NSObject
    
    @property (nonatomic, copy) requestCompletedBlock completed;
    @property (nonatomic, copy) requestErrorBlock errored;
    
    - (void)makeRequest:(requestCompletedBlock)completed error:(requestErrorBlock)error;
    
    @end
    

    And the implementation of that might look something like this:

    @implementation RequestClass
    
    @synthesize completed = _completed;
    @synthesize errored = _errored;
    
    // Connection Logic, etc.
    
    - (void)makeRequest:(requestCompletedBlock)completed error:(requestErrorBlock)error
    {
        self.completed = completed;
        self.errored = error;
        // Initiate the request...
    }
    
    -(void) connectionDidFinishLoading:(NSURLConnection *) connection 
    {
        NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:[webData length] encoding:NSUTF8StringEncoding];
    
        // Processing, etc.
    
        // Here we'll call the delegate with the result:
        self.completed(theResult);
    }
    
    @end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok sorry this might seem like a dumb question but I cannot figure this
Ok so this might be a dumb question, but I can't seem to figure
This might be a dumb question, but maybe someone can provide some insight. I
This might sound like a reaaaally dumb question but... why do browsers have a
Ok, this might just be a dumb question, but I was wondering if Bluetooth
This might be an interesting question. I need to test that if I can
This might sound like a little bit of a crazy question, but how can
This might be a dumb question, but I don't get it: I have a
This might seem like a really dumb question, but I am writing an application
This might sound very dumb question but I am just confused. I am quite

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.