{"id":186599,"date":"2024-01-31T23:33:49","date_gmt":"2024-01-31T21:33:49","guid":{"rendered":"https:\/\/koninis.me\/?p=186599"},"modified":"2024-02-01T08:18:01","modified_gmt":"2024-02-01T06:18:01","slug":"new-behaviour-for-urlstring-initializer-in-ios-17","status":"publish","type":"post","link":"https:\/\/koninis.me\/index.php\/2024\/01\/31\/new-behaviour-for-urlstring-initializer-in-ios-17\/","title":{"rendered":"New behaviour for URL(string:) initializer in iOS 17"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>    let url = URL(string: \"invalid url\")<br>    \/\/ in iOS &gt;=17 this is a valid URL object with value invalid%20url<br>    \/\/ in iOS &lt;17, MACOS 13.6, Linux this is nil<br><\/code><\/pre>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\">In the <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/url\/3126806-init\">documentation<\/a> it seems that<em> for apps linked on or after iOS 17 and aligned OS versions, <a href=\"https:\/\/developer.apple.com\/documentation\/foundation\/url\"><code>URL<\/code><\/a> parsing has updated from the obsolete RFC 1738\/1808<\/em><\/p>\n\n\n\n<figure class=\"wp-block-gallery has-nested-images columns-default is-cropped wp-block-gallery-1 is-layout-flex wp-block-gallery-is-layout-flex\">\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"660\" height=\"222\" data-id=\"186600\" src=\"https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/cocoapods_issue.png?resize=660%2C222&#038;ssl=1\" alt=\"\" class=\"wp-image-186600\" srcset=\"https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/cocoapods_issue.png?resize=1024%2C344&amp;ssl=1 1024w, https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/cocoapods_issue.png?resize=300%2C101&amp;ssl=1 300w, https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/cocoapods_issue.png?resize=768%2C258&amp;ssl=1 768w, https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/cocoapods_issue.png?w=1232&amp;ssl=1 1232w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" decoding=\"async\" width=\"660\" height=\"246\" data-id=\"186601\" src=\"https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/Screenshot-2023-09-28-at-4.31.09-PM.png?resize=660%2C246&#038;ssl=1\" alt=\"\" class=\"wp-image-186601\" srcset=\"https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/Screenshot-2023-09-28-at-4.31.09-PM.png?resize=1024%2C382&amp;ssl=1 1024w, https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/Screenshot-2023-09-28-at-4.31.09-PM.png?resize=300%2C112&amp;ssl=1 300w, https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/Screenshot-2023-09-28-at-4.31.09-PM.png?resize=768%2C287&amp;ssl=1 768w, https:\/\/i0.wp.com\/koninis.me\/wp-content\/uploads\/2024\/01\/Screenshot-2023-09-28-at-4.31.09-PM.png?w=1200&amp;ssl=1 1200w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/figure>\n<\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">I found out this by accident from a failing unit test, that checked the expected behaviour when the a stirng is not valid URL.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To get the old behaviour you will need to use the URL(string:encodingInvalidCharacters:) initialiazer, that is ofcourse available only in iOS &gt;=17. <\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_crdt_document":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-186599","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/posts\/186599","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/comments?post=186599"}],"version-history":[{"count":6,"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/posts\/186599\/revisions"}],"predecessor-version":[{"id":186607,"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/posts\/186599\/revisions\/186607"}],"wp:attachment":[{"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/media?parent=186599"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/categories?post=186599"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/koninis.me\/index.php\/wp-json\/wp\/v2\/tags?post=186599"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}