• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString stringWithContentsOfURL
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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] initWithData: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] initWithData: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.

References: 
 >NSString stringWithContentsOfURL (From: SoK <email@hidden>)

  • Prev by Date: RE: [Q] How to frame NSTableView and NSOutlineView?
  • Next by Date: Re: Proper retain/release etiquette
  • Previous by thread: NSString stringWithContentsOfURL
  • Next by thread: Creating an automatic build number with PBX?
  • Index(es):
    • Date
    • Thread