When dealing with Unicode Characters in URL strings, we need to encode Unicode characters to percentages for them to be readable by URL. We do so using the following code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://hashaam.com/2017/11/13/url-encode-only-unicode-characters/ | |
let originalURLString = "https://www.exampleserver.com/articles/latest/لن-تصدق-ما-يمكن-أن-تجده-في-مطبخك-فاحذر-غسل-هذه-الأداة.html" | |
let allowedCharacterSet = CharacterSet.urlFragmentAllowed | |
let urlString = originalURLString.addingPercentEncoding(withAllowedCharacters: allowedCharacterSet)! | |
let url = URL(string: urlString)! |