0

I need to pass some parameters from iOS system to the server.

one of those parameters is timezone, and I need to include the sign in front of the hour value like +02:00

I tried this:

let url = "http://my_server/my_page.php?timezone=+02:00"
let urlEncoded = url.stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

But unfortunately didn't work. And at the server I got " " space instead of the "+" sign. (PHP used on the server)

I saw Objective-C url encoding but really I don't need to encode http://.., I only need the + sign in the parameters list.

Also I look at Swift - encode URL, But when I send + sign, PHP server will omit it.

Community
  • 1
  • 1
Wajih
  • 4,227
  • 2
  • 25
  • 40
  • 3
    use `%2B` instead of `+` sign. – Sunny Jain Jun 21 '16 at 07:48
  • Maybe answers for [this questions](http://stackoverflow.com/questions/898488/make-an-nsurl-with-an-encoded-plus-2b) will be helpful. – tahavath Jun 21 '16 at 07:49
  • You need to encode the query part of the url not the entire url, so you need to encode the query part first then compose the result into the url. – KudoCC Jun 21 '16 at 08:15
  • The way you need to use encoding functions depends on implementation of your networking client. In general it is possible to safely encode individual parameters, in your case only the part "+02:00" needs to be encoded. – A-Live Jun 21 '16 at 08:16
  • Thanks all, @SunnyJain you can post your answer and I will accept it. Can you please put the `-` equivalent also ? – Wajih Jun 21 '16 at 08:24
  • 1
    @wajeeh `-` is an unreserved character, it needn't be encoded. Check [this link](https://en.wikipedia.org/wiki/Percent-encoding) for more detail. – KudoCC Jun 21 '16 at 08:39
  • 1
    @wajeeh : You can see this [link](http://www.w3schools.com/tags/ref_urlencode.asp) `%2D` is used for `-` – Sunny Jain Jun 24 '16 at 05:38

0 Answers0