0

Need help to replace my spaces from the textfield entry for posting to a GET

My current code:

    NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", messageBox.text];
NSURL *add = [NSURL URLWithString:urlString];

[messageView loadRequest:[NSURLRequest requestWithURL:add]];

However I tried

NSString *finalAdd = [add stringByReplacingOccurrencesOfString:@" " withString:@"+"];
Emil Elkjær
  • 685
  • 1
  • 9
  • 31
  • possible duplicate of [URL decoding/encoding NSString](http://stackoverflow.com/questions/6688287/url-decoding-encoding-nsstring) – Mike Weller Oct 18 '12 at 13:13

2 Answers2

1

You should escape the parameter

NSString* urlString = [NSString stringWithFormat:@"http://server.com/ios/add.php?user=iPhone+App&message=%@", [messageBox.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *add = [NSURL URLWithString:urlString];
rckoenes
  • 69,092
  • 8
  • 134
  • 166
Edwin Iskandar
  • 4,119
  • 1
  • 23
  • 24
0

Check out this post or this SO question. Both offer good solutions for encoding urls.

Community
  • 1
  • 1
coder
  • 10,460
  • 17
  • 72
  • 125