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
  • 667
  • 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
  • 68,719
  • 8
  • 132
  • 164
Edwin Iskandar
  • 4,109
  • 1
  • 22
  • 24
0

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

Community
  • 1
  • 1
coder
  • 10,236
  • 17
  • 70
  • 124