Categories
Foundation iOS Swift

Changes to URL string parsing in iOS 17

If we go to Apple’s documentation for URL’s init(string:) method, then this comes with a huge yellow banner describing new behaviour in iOS 17.

For apps linked on or after iOS 17 and aligned OS versions, URL parsing has updated from the obsolete RFC 1738/1808 parsing to the same RFC 3986 parsing as URLComponents. This unifies the parsing behaviors of the URL and URLComponents APIs. Now, URL automatically percent- and IDNA-encodes invalid characters to help create a valid URL.

https://developer.apple.com/documentation/foundation/url/3126806-init

Switching to the newer URL specification is a big deal, and in addition, the last sentence says that the new default is that URL(string:) tries to encode invalid characters. This is a big deal. This was not a case before. If we want to keep using the pre-iOS 17 behaviour, then we would need to replace URL(string:) with the new URL(string:encodingInvalidCharacters:) and passing false to the second argument. Apps which deals with URL strings definitely need to be tested thoroughly on iOS 17.

// iOS 16
URL(string: "my string") -> nil
// iOS 17
URL(string: "my string") -> my%20string
but
URL(string: "my string", encodingInvalidCharacters: false) -> nil 

If this was helpful, please let me know on Mastodon@toomasvahter or Twitter @toomasvahter. Feel free to subscribe to RSS feed. Thank you for reading.

2 replies on “Changes to URL string parsing in iOS 17”