After sending a HTTP request from Swift, I get a field in the response called textEncodingName
.
I want to convert the data
object I also received into a string containing its contents, and to do this, I'm using String(data: data!, encoding: .utf8)
. This works most of the time, because most websites are UTF-8 encoded. But with, for example, https://www.google.co.uk
, the response.textEncodingName == "iso-8859-1"
.
I guess other websites would use even more obscure encodings, so my question is this: how can I find the right encoding to convert my data
object to the correct string.