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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T19:02:22+00:00 2026-05-28T19:02:22+00:00

I was recently looking at an example from apple about NSURLConnection and I tried

  • 0

I was recently looking at an example from apple about NSURLConnection and I tried implementing it into my code but I am not sure if am I doing it right.

Basically I want the connection to go to my website where I have it connected to a php script that runs the search within my database and then echo’s it to the browser. I want the iphone to take the line that is echoed and hold it into a string variable. This is my code.
Is this correctly done?

Thank you in advance

  NSString *stringToBeSent= [[NSString alloc] initWithFormat:
    @"http:/xxxxx/siteSql.php?  data=%@",theData];

      NSURLRequest *theRequest=[NSURLRequest requestWithURL:
      [NSURL URLWithString:stringToBeSent]
      cachePolicy:NSURLRequestUseProtocolCachePolicy
     timeoutInterval:60.0];


   // create the connection with the request
    // and start loading the data
 NSURLConnection *theConnection=[[NSURLConnection alloc] 
 initWithRequest:theRequest  delegate:self];


   if (theConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere... in my .h file
    // NSMutableData *receivedData; 

    receivedData = [[NSMutableData data] retain];

     //convert NSMutableData to a string
    NSString *stringData= [[NSString alloc] 
      initWithData:receivedData encoding:NSUTF8StringEncoding];

    NSLog (@"result%@", receivedData);

    } else {
    // Inform the user that the connection failed.


    NSLog(@"failed");

      }
  • 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-28T19:02:25+00:00Added an answer on May 28, 2026 at 7:02 pm

    I think you might be missing a couple things:

    1. In the method that you use to trigger retrieving the data make sure you release the old data before initializing:

      [retrievedData release];
      retrievedData=[[NSMutableData alloc] init];
      
    2. I assume that space is a typo or something for the URL?

    3. You don’t need to call requestWithURL:cachePolicy:timeoutInterval: requestWithURL: uses the same defaults as you chose.

    4. The data will come in blocks. You’ve got to handle that over time, outside this method, using the delegate method connection:didReceiveData:, like so:

      - (void)connection:(NSURLConnection *)conn didReceiveData:(NSData *)data
      {
           [receivedData appendData:data];
      }
      
    5. Similarly, if you want something done with the data once it’s all received, you do it in connectionDidFinishLoading: NOTE THAT THE CONNECTION IS RELEASED so it has to be defined in your header as an instance variable (eg. NSURLConnection *connection;

      - (void)connectionDidFinishLoading:(NSURLConnection *)conn
      {
         NSString *stringData= [[NSString alloc] 
         initWithData:receivedData encoding:NSUTF8StringEncoding]; 
         NSLog(@"Got data? %@", stringData);
         [connection release];
          connection = nil;
         // Do unbelievably cool stuff here //
      }
      
    6. Also look into the other delegate methods like connection:didFailWithError: You probably want to release the connection and stringData there as well, in case of an error.

    I hope that’s of some help! Enjoy!

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

Sidebar

Related Questions

Recently I've been looking at the some of the C example code from the
I just recently started looking into WatiN and was following the example from http://www.codeproject.com/KB/aspnet/WatiN.aspx
I am not an expert on VBA as I've recently started looking into this.
Quick question about something I've ran into alot recently: So many times I'm looking
I recently started looking into building web applications using .NET MVC and I stumbled
Recently I was looking at some source code provided by community leaders in their
recently, i am looking into assembly codes for #define, const and enum: C codes(#define):
I have been looking into CruiseControl configuration recently (I'm a complete CC noob) and
I've been looking into SQL recently and exploring a bit. in regards to Temp
I've recently started looking into the MapReduce/Hadoop framework and am wondering if my problem

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.