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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:21:34+00:00 2026-05-16T12:21:34+00:00

I have a NSURLConnection that receives data output from a url pointing to a

  • 0

I have a NSURLConnection that receives data output from a url pointing to a php script on my server.

Most of the time everything works fine and the data is retrieved in its complete form.

However, sometimes I receive NULL or broken (i.e. the bottom half) of data at:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 

When this happens, if I reload the connection it will always return the same null or broken block
of data for the request.

EDIT*:
I’ve realized that when I receive what I thought was nil data, I actually received data
but the NSString created from this data is nil. I still don’t understand why though. My php encoding output is always UTF-8 so I don’t think it is an issue of encoding and besides it works most of the time with this.

I have checked the php script with that same request to verify that it is not a problem on the server side or with the php script and confirmed that it is NOT.

My code is Below:

-(void)setUpConnectionAndMakeRequest {

            NSString *URLpath = @"http://www.example.com/myphp.php"; 
    NSURL *myURL = [[NSURL alloc] initWithString:URLpath];

    NSMutableURLRequest *myURLRequest = [NSMutableURLRequest requestWithURL:myURL cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:60];
    [myURL release];
    [myURLRequest setHTTPMethod:@"POST"];


    //I added this because I thought it may be a problem relating to cache but it isn't
    NSURLCache *cache = [NSURLCache sharedURLCache]; 
    [cache removeAllCachedResponses];


    NSString *httpBodystr = [NSString stringWithString:@"command=runscript"];


    [myURLRequest setHTTPBody:[httpBodystr dataUsingEncoding:NSUTF8StringEncoding]];


    [mailData setData:nil]; //mailData is a NSMutableData object which accumulates the data retrieved by the request

    [NSURLConnection connectionWithRequest:myURLRequest delegate:self];

}


- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {

NSString *untrimmedDataSTR = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];  //Created so I can see the data (always text) in NSLog
NSLog(@"Live Data: %@", untrimmedDataSTR);   //This is where I see that the data is broken or null when it shouldn't be


[mailData appendData:data];  //Append accumulated data to NSMutableData object used later in my app.

[untrimmedDataSTR release]; 

}

Any help would be much appreciated.

  • 1 1 Answer
  • 3 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-16T12:21:34+00:00Added an answer on May 16, 2026 at 12:21 pm

    According to the NSString reference, -initWithData:encoding: returns nil if “the initialization fails for some reason (for example if data does not represent valid data for encoding).”

    That almost certainly means that the response from the server is not, in fact, UTF-8 encoded data.

    The way to check would be to NSLog the data before trying to convert to an NSString:

    NSLog(@"Raw Data: %@", data);
    

    (The -description method on NSData will return a hexadecimal representation of the contents; that’s what will get logged).

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

Sidebar

Related Questions

I have implemented an NSURLConnection that sends a request to a server and receives
An iPhone question for you guys! I have an NSURLConnection that downloads XML from
I'm loading data from a server and processing the response asynchronously. Using NSURLConnection. Each
I have data returned from a service that is rendered to my UITableView several
I have a class that is derived from NSURLConnection. Curiously, description and dealloc fail
We have a large project that needs to sync large files from a server
I have a NSURLConnection which is a post to the server, but I expect
I have been playing around with NSURLConnection. Now I'm trying to grab some data
I have create a class MyConnection that extends NSURLConnection. I have implemented some delegate
I have a class with a method (getAndPlaySong) that gets an text file from

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.