New behaviour for URL(string:) initializer in iOS 17

It is weird to have breaking changing in the bahaviour of the Foundation library APIs depending on the target platform. Before iOS 17 the URL(string:) initializer failed when the string argument was not a valid URL.

    let url = URL(string: "invalid url")
// in iOS >=17 this is a valid URL object with value invalid%20url
// in iOS <17, MACOS 13.6, Linux this is nil

In the documentation it seems that for apps linked on or after iOS 17 and aligned OS versions, URL parsing has updated from the obsolete RFC 1738/1808

I found out this by accident from a failing unit test, that checked the expected behaviour when the a stirng is not valid URL.

To get the old behaviour you will need to use the URL(string:encodingInvalidCharacters:) initialiazer, that is ofcourse available only in iOS >=17.

Leave a Reply

Your email address will not be published. Required fields are marked *