The URL
will only support ASCII characters, unicode characters will need to be encoded.
After a bit of googling and playing around, I was able to get...
var components = URLComponents()
components.scheme = "kotoba"
components.host = "dictionary"
let queryItem = URLQueryItem(name: "word", value: "犬")
components.queryItems = [queryItem]
let url = components.url
to produce Optional(kotoba://dictionary?word=%E7%8A%AC)
(you can unwrap it)
(credit to Swift - encode URL)
In my testing I also tried using let escapedString = "犬".addingPercentEncoding(withAllowedCharacters: .urlHostAllowed)
which also produced "%E7%8A%AC"
I've not tried opening the URL
, but at least it's no longer nil