Pound symbol added to URL in ios 14

Viewed 338

I have a running app in a webserver, that redirects to an app installed on a iOS 14 device. the url is in the form of

myapp://?p=someBase64EncodedString

and the application is decoding the string. after upgrading to iOS 14, the url the app gets is

myapp://?p=someBase64EncodedString# and the pound symbol added to the end of the string fails to decode on my device. When using ignoreUnknownCharacters everything works fine, but where did this # come from?

3 Answers

As @jtbandes mentioned in the comments, # is valid character in a URL.

To avoid this kind of issues, the safest way to parse a URL is by using URLComponents, following this answer.

Here is an example:

let urlString = "https://test.host.com:8080/Test/Path?test_parameter=test_value#test-fragment"
let urlComponents = URLComponents(string: urlString)

print("scheme: \(urlComponents?.scheme ?? "nil")")
print("host: \(urlComponents?.host ?? "nil")")
print("port: \(urlComponents?.port ?? -1)")
print("path: \(urlComponents?.path ?? "nil")")
print("query: \(urlComponents?.query ?? "nil")")
print("queryItems: \(urlComponents?.queryItems ?? [])")
print("fragment: \(urlComponents?.fragment ?? "nil")")

This will have as output:

scheme: https
host: test.host.com
port: 8080
path: /Test/Path
query: test_parameter=test_value
queryItems: [test_parameter=test_value]
fragment: test-fragment

Could Swift 5 and raw strings be the culprit? My guess is that the application is using # as a custom string delimiter and on the decoding is replacing some special character with an #.

It may be an api action in a redirect for an api you used. If so, it is out of your control and potentially scope of access.

Related