Search code examples
iosencodingnsstringnsurlconnectionnsdata

Why would NSURL Connection Received Data contain bytes, but on conversion to NSString, the string is Null?


I have tried a lot of other solutions from other posts, but have not successfully got a string from my received data. Except I am running almost a replica on another file and it works 100% of the time. It even gets the data from the exact same php file.

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

    [imagesTableData appendData:data];
    NSString *dataStringImages = [[NSString alloc] initWithData:imagesTableData encoding:NSUTF8StringEncoding];
    receivedDataImages = [NSString stringWithFormat:@"%@%@",receivedDataImages,dataStringImages];

}

+ (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"%lu -- %@",(unsigned long)[imagesTableData length],receivedDataImages);
}

NSLog Output: 14938 -- (null)

enter image description here

I have fixed my issue by changing the encoding:NSUTF8StringEncoding to encoding:NSASCIIStringEncoding. See my fix for didReceiveData below:

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

    [imagesTableData appendData:data];
    NSString *dataStringImages = [[NSString alloc] initWithData:imagesTableData encoding:NSASCIIStringEncoding];
    receivedDataImages = [NSString stringWithFormat:@"%@%@",receivedDataImages,dataStringImages];

}

Now I am left with why was this the fix? Let me explain what is making this so perplexing to me. I have two different but similar utility files that I am using to receive data for parsing and I then in put them into a database.

The issue is that both files connect to the same PHP file and that PHP file outputs data the exact same way with different amounts of data. Why would one need an encoding of "NSUTF8StringEncoding" and the other "NSASCIIStringEncoding"?

See my code and the related data output below for each utility file.

Utility File A

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

    [placesTableData appendData:data];
    NSString *dataStringPlaces = [[NSString alloc] initWithData:placesTableData encoding:NSUTF8StringEncoding];
    receivedDataPlaces = [NSString stringWithFormat:@"%@%@",receivedDataPlaces,dataStringPlaces];

}

Utility Data A

MERRYMAN|Merryman Performing Arts Center|LOC|40.698903|-99.085779|||Mention this coupon when ordering for 20% off tickets at the Merryman Performing Arts Center.|308-698-8297||http://www.merrymancenter.org|225 West 22nd Street Kearney, NE 68845||0000-00-00 00:00:00|0000-00-00 00:00:00|0|2014-07-16 21:29:51~~

Utility File B

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

    [imagesTableData appendData:data];
    NSString *dataStringImages = [[NSString alloc] initWithData:imagesTableData encoding:NSASCIIStringEncoding];
    receivedDataImages = [NSString stringWithFormat:@"%@%@",receivedDataImages,dataStringImages];

}

Utility Output B

WSGFFJKM|LIPS Printing Service 25% Off|LIPSNewCustomerAd2014_KearneyApp.jpg||LIPSPRINTING2|2014-07-16 23:46:07~~

If you have any input as to how or if I should rewrite my question, please suggest in comments.


Solution

  • From the documentation for initWithData:encoding::

    Returns nil if the initialization fails for some reason (for example if data does not represent valid data for encoding).

    Double check that your data is of the correct encoding or a string at all. You might want to put off converting the data to string until after the connection is finished loading as well.