URL(string:) returns nil for a path that contains cyrillic

Anton Tropashko

[NS]URL(string:) returns nil for a path that contains cyrillic

https://somedomain.ru/upload/files/Оферта.pdf

safari in 10.15 opens that url just fine. Is there a workaround that?

I do it like so:

public extension URL {
    static func open(urlString: String) {
        guard let url = NSURL(string: urlString) else {
            return
        }
        UIApplication.shared.open(url as URL)
    }
}

and never reach UIApplication.shared.open(url as URL) (or UIApplication.shared.open(url) if I do URL instead of NSURL I suppose the two are bridged)

UPD the resolution thanks to Daniel Storm:

static let оферта = "https://somesiteofours.ru/upload/files/" + ("Оферта.pdf".addingPercentEncoding(withAllowedCharacters: .urlPathAllowed) ?? "404")

...

Daniel Storm

You’ll need to URL encode the path:

https://somedomain.ru/upload/files/%D0%9E%D1%84%D0%B5%D1%80%D1%82%D0%B0.pdf

Apple docs: addingPercentEncoding(withAllowedCharacters:)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related