moshikafya
moshikafya

Reputation: 3320

issue with converting data from server (PHP) to NSString

Ok, so I am very very new to iOS development and also to PHP (well, I know practically nothing about PHP) but I am trying to do the following: my app uses an external key that it retrieves from my server and assign it to an NSString. The problem is that the string gets extra link break (\n) and an extra space and its very annoying to parse. I am guessing that I am doing something wrong cause I don't see the logic in this. Can someone please explain how I could avoid that?

Here is my code:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setTimeoutInterval:180.0]; 
[request setURL:[NSURL URLWithString:urlString]]; //url to the getkey.php file 
[request setHTTPMethod:@"POST"];

NSString *key = [[NSString alloc] initWithData:[NSURLConnection    sendSynchronousRequest:request returningResponse:nil error:nil] encoding:NSUTF8StringEncoding];

The PHP file (silly i know, but suppose to do the job, and again, i know nothing about PHP so thats good for me):

<?php

 echo 23204239423;

?> 

"key" gets 23204239423 with an extra space and \n.

Upvotes: 1

Views: 145

Answers (1)

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181350

You probably have a space after ?> and also a \n (new line). If you remove those from your PHP file, you will probably avoid getting them on HTTP request.

Having said this, I would probably trim the string from NSString just to avoid having the HTTP server send exactly what the iOS application is expecting. I thing your mobile application parser should be a little bit more flexible with data it's receiving.

Upvotes: 3

Related Questions