Re: NSString stringWithContentsOfURL
Re: NSString stringWithContentsOfURL
- Subject: Re: NSString stringWithContentsOfURL
- From: Pierre-Olivier Latour <email@hidden>
- Date: Tue, 23 Jul 2002 14:42:53 +0200
>
Hi
>
>
I have made a small application to retrieve news from a site to a dock
>
menu ... but i have a problem ... when i get the source of the page
>
caractere are encoded ...
>
>
i use this :
>
>
NSString *page = [NSString stringWithContentsOfURL:[NSURL
>
URLWithString:@"http://www.trucmuche.com"]];
>
>
but i get some things like this : "Transformer un bo\\323tier SCSI en
>
Firewire"
StringWithContentsOfURL assumes the web page is formatted as
NSMacOSRomanEncoding, which is not always the case. Most often, it is
encoded as NSISOLatin1StringEncoding.
Use this method instead:
- (NSString*) downloadWebPage:(NSString*)url
{
NSData* data;
NSString* encodedString;
data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
if(data == nil)
return nil;
encodedString = [[[NSString alloc] initWith
Data:data
encoding:NSISOLatin1StringEncoding] autorelease];
[data release];
return encodedString;
}
If you need to explicitely converts the string to NSMacOSRomanEncoding, use:
data = [encodedString dataUsingEncoding:NSMacOSRomanStringEncoding
allowLossyConversion:YES];
[encodedString release];
encodedString = [[NSString alloc] initWith
Data:data encoding:
NSMacOSRomanStringEncoding];
_____________________________________________________________
Pierre-Olivier Latour email@hidden
Lausanne, Switzerland
http://www.pol-online.net
_______________________________________________
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.