0

I ran into a weird problem. I built a mysql database where the user can store his contacts through an app. He just adds his contact information by clicking on a button. Then, the data is added to the database.

However, when the contact's information has spaces between his first name, last name or number the app refuses to accept the data. I dont get an error message or anything like that, but I also dont get any confirmation. The record does not exist in the database.

There is no problem with my php file (The file is definitely correct, I got it checked here, twice). Here is my code:

NSString *strURL = [NSString stringWithFormat:@"http://www.abc.com/phpFile.php?number=%@&name=%@&lastname=%@", number, firstName, lastName];

NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
        NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

        NSLog(@"%@", strResult);
Paul Nikonowicz
  • 3,883
  • 21
  • 39
Blade
  • 1,435
  • 1
  • 14
  • 22

2 Answers2

5

The space character is not allowed in a URI. Use %20 to encode the space. There are other characters that are also not allowed as part of the URI. Look up URI encoding.

bbarnhart
  • 6,620
  • 1
  • 40
  • 60
  • Thanks. I was thinking the same thing. But I have no idea where I could put it in my code, because I can't know where the spaces will be when someone uses the app. – Blade Feb 08 '12 at 15:21
  • 1
    Look at the following link for how to encode: [link](http://stackoverflow.com/questions/3140424/stringbyaddingpercentescapesusingencoding-not-working-with-nsstrings-with-0) – bbarnhart Feb 08 '12 at 15:35
2
NSString *strURL = [NSString stringWithFormat:@"http://www.abc.com/phpFile.php?number=%@&name=%@&lastname=%@", number, firstName, lastName];
strURL  = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]

NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
        NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];

        NSLog(@"%@", strResult);
NeverBe
  • 5,213
  • 2
  • 25
  • 39