0

I'm developing an iOS app and trying to create an NSURL for a PHP page, and I need to escape the apostrophe's for PHP with: \' Below is an example of a URL I tried to escape the apostrophe :

http:/www.name.com/receive.php?name=hello\'s

The problem is that the backslash character isn't allowed in NSURLs. And before anyone says, I already tried "\ \'" so that isn't the issue. The issue is with the backslash itself, I guess they just simply aren't allowed in NSURLs. How can I get around this?

pwoerfulafhjksdh
  • 381
  • 1
  • 3
  • 11

1 Answers1

0

Try this...

NSString *s=@"http:/www.name.com/receive.php?name=hello\'s";
s=[s stringByReplacingOccurrencesOfString:@"\'" withString:[NSString stringWithFormat:@"\\"]];

NSLog(@"s=%@",s);
NSURL *url=[NSURL URLWithString:s];

NSLog(@"url=%@",url);
Ashok Londhe
  • 1,491
  • 11
  • 29
  • 1
    That doesn't work. I already said so in my description. The problem is with the backslash itself. NSURL does not permit it. – pwoerfulafhjksdh May 22 '15 at 04:01
  • That's not going to help. All you did is replace the backslash and apostrophe with nothing. I need to keep the backslash, not get rid of it. – pwoerfulafhjksdh May 22 '15 at 04:05
  • @pwoerfulafhjksdh You can use encoding for the url string like this way `NSString *str = @""; NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];` – Vijay Masiwal May 22 '15 at 04:08
  • @VijayMasiwal - Yes, `stringByAddingPercentEscapesUsingEncoding` is better than this backslash silliness, which is entirely misguided. But it won't percent escape the `'` character. He really should use `stringByAddingPercentEncodingWithAllowedCharacters` (using alphanumeric plus `-`, `.`, `_`, and `~`) or use [`CFURLCreateStringByAddingPercentEscapes`](http://stackoverflow.com/questions/20777112/urlencode-in-objective-c/20777164#20777164). Unfortunately, `stringByAddingPercentEscapesUsingEncoding` eliminates the `nil` result for `NSURL`, but doesn't solve the deeper problem here. – Rob May 22 '15 at 05:00
  • @Rob Thanks you for your suggestion. Actually i have not study so much on this topic. so i use \ and it works so i posted my answer. if you know the better you can feel free to edit my answer and guide me if i am doing any mistake. – Ashok Londhe May 22 '15 at 05:04
  • The OP is, unfortunately, conflating two completely different issues, namely the need to escape `'` characters in string values in his PHP code, and the proper way to send `'` character in a value in a request (which is governed by [RFC 3986](http://tools.ietf.org/html/rfc3986)). The whole backslash aspect of his question is, with all due respect, a complete misunderstanding on his part. – Rob May 22 '15 at 05:19
  • @Rob can you please correct my answer. and also explain me how it work. – Ashok Londhe May 22 '15 at 05:21
  • @AshokLondhe Since this question was closed, I posted an answer under his duplicate question. See http://stackoverflow.com/a/30389018/1271826. – Rob May 22 '15 at 05:33