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 6204535
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T05:06:40+00:00 2026-05-24T05:06:40+00:00

My iPhone app sends a message to a server via HTTP post. I would

  • 0

My iPhone app sends a message to a server via HTTP post. I would like to make use of the NSURLConnection Delegate for several reasons.

Here is the code. What should I do to make it use a delegate?

//.h
#import <Foundation/Foundation.h>

@interface ServerConnection : NSObject <NSURLConnectionDelegate>

-(NSData*) postData: (NSString*) strData;

//delegate method
- (void)connectionDidFinishLoading:(NSURLConnection *)connection;

@end

and the .m (the important part is the second method)

//.m

#import "ServerConnection.h"

@implementation ServerConnection

- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
    return self;
}

-(NSData*) postData: (NSString*) strData //it's gotta know what to post, nawmean?
{

    //postString is the STRING TO BE POSTED
    NSString *postString;

    //this is the string to send
    postString = @"data=";
    postString = [postString stringByAppendingString:strData]; 

    NSURL *url = [NSURL URLWithString:@"//URL GOES HERE"];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    NSString *msgLength = [NSString stringWithFormat:@"%d", [postString length]];

    //setting prarameters of the POST connection
    [request setHTTPMethod:@"POST"];
    [request addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request addValue:msgLength forHTTPHeaderField:@"Content-Length"];
    [request addValue:@"en-US" forHTTPHeaderField:@"Content-Language"];
    [request setHTTPBody:[postString dataUsingEncoding:NSUTF8StringEncoding]];
    [request setTimeoutInterval:20.0];

    NSLog(@"%@",postString);

    NSURLResponse *response;
    NSError *error;

    //this sends the information away. everybody wave!
    NSData *urlData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

    return urlData;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
//delegate
{
    NSLog(@"connectionDidFinishLoading!!!");
}

@end

Again, the question is what should I do to make it use a delegate? I need to know when I am done with the connection.

As a side note, I’m using ARC so you don’t worry about my memory management. 😉

  • 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-24T05:06:40+00:00Added an answer on May 24, 2026 at 5:06 am

    You’re using the synchronous API, so when the method returns the connection has already finished. You only need the delegate if you’re using the asynchronous API which you can use with any of +connectionWithRequest:delegate:, –initWithRequest:delegate:, or –initWithRequest:delegate:startImmediately:.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am new to use Facebook API in iphone app.I am trying to make
there is a error in my iPhone app. I use the CameraPicker to make
I have an app (iPhone) with 2 Xibs that I would like to localize,
I've got an iPhone App that is supposed to send POST data to my
How would I send a message to an App on my mac (which I
I'd like to allow users to send out Tweets from my iPhone app. That's
I am developping an iPhone App that sends SMS, I am using XCode 4.2
Im writing a server application for my iPhone app. The section of the server
I'm working on an iPhone app that performs some communication with the server to
I have couple of iPhone apps that sends an email via code. I've noticed

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.