0

I have these lines in my ios app developed on xcode 12.5 using swift:

    var urlString = "https://www.weather.ca/?polygon=CZWG|fir|1621169170407"

    let url = URL(string: urlString)

The problem is that url returns nil. If I remove the | characters the url looks right (identical to urlString).

Any idea how I can use the "|" character in the URL?

I tried the following code but the JSON returned is different so I don't think this data source handles "|" character the same as "%7c"

    let urlString = "https://www.weather.ca/?polygon=CZWG|fir|1621169170407"
    
    let csCopy = CharacterSet(bitmapRepresentation: CharacterSet.urlPathAllowed.bitmapRepresentation)
    var urlString2 = urlString2.addingPercentEncoding(withAllowedCharacters: csCopy)
    let url = URL(string: urlString2!)
Guy Fisher
  • 31
  • 4
  • I think the duplication was a bit premature, but fundamentally, `|` is not valid in a URl. You need to have it percent encoded to `%7C` (IDK if `%7c` is valid, I'd have to look it up). If the web API you're targetting doesn't support that, that's a fundamental issue in that API. – Alexander May 23 '21 at 20:50
  • Hello. Yes, thank you, Alexander. Replacing the "|" character with "%7C" remedied the problem. Ironically, when original API URL is with those characters – Guy Fisher May 24 '21 at 21:42

0 Answers0