Re: Converting encodingName to NSStringEncoding
Re: Converting encodingName to NSStringEncoding
- Subject: Re: Converting encodingName to NSStringEncoding
- From: j o a r <email@hidden>
- Date: Fri, 9 Jan 2004 21:28:08 +0100
On 2004-01-09, at 13.30, Patrick Machielse wrote:
>
I am using an NSURLConnection and want to convert the returned NSData
>
to an
>
NSString (I know that the data is text). To do this I need to know the
>
NSStringEncoding of the data. NSURLResponse gives me the
>
'textEncodingName',
>
which is the exact string passed by the server. This string must be
>
queried
>
to establishe the appropriate encoding. So far I've come up with the
>
following solution.
>
>
{
>
// some initialisation of variables and setup
>
// ...
>
>
NSData *d = [NSURLConnection sendSynchronousRequest:request
>
returningResponse:&response
>
error:&error];
>
>
NSString *ename = [response textEncodingName];
Notice that this might not always return a text encoding, perhaps it
even most often will not.
My suggestion is that you check if it does, and if so use that
information, something like what Aki suggested. However, if you don't
get a valid encoding this way, you might look at this very, very,
convenient method in WebCore to help you detect the proper encoding in
a downloaded NSData, and convert it into a NSString:
NSString *htmlString = [WebCoreEncodings decodeData: htmlData];
>
I'm not an expert in text encoding and conversion (). It seems Carbon
>
has
>
more functionality in this respect?
Carbon has something called the "TextEncodingConverter", which at least
in my tests is _completely_ _useless_ for detecting the encoding in
HTML data.
j o a r
[demime 0.98b removed an attachment of type application/pkcs7-signature which had a name of smime.p7s]
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.