Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"]; NSError *error = nil; NSString *my_string = [[NSString alloc] initWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error];

I get a empty my_string variable.

I tried the initWithContentsOfURL: method (which is deprecated in iOS 2.0) and I get the content of my page. But I still need to specify a encoding language.

What's wrong ?

Thanks :)

Here is the error message : Error Domain=NSCocoaErrorDomain Code=261 "The operation couldn’t be completed. (Cocoa error 261.)" UserInfo=0x6a10230 {NSURL= my_url.com/my_file.xml , NSStringEncoding=4} Pierre Espenan Oct 22, 2010 at 10:34

the encoding of your file is probably not UTF8.

If you don't know the encoding of your file, you could try this method:

- (id)initWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error

you have to pass a pointer to a NSStringEncoding, like you did with error.:

NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"];
NSError *error = nil;
NSStringEncoding encoding;
//NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
//                                                     encoding:NSUTF8StringEncoding
//                                                        error:&error];
NSString *my_string = [[NSString alloc] initWithContentsOfURL:url
                                                 usedEncoding:&encoding 
                                                        error:&error];

after this your encoding is present in the encoding variable. If you are not interested in the used encoding, I guess you could pass NULL as pointer as well.

I tried to change UTF8 to ISOLatin1 (ISOLatin2 works as fine as UTF8). It partially worked : I still have issues with î, ', ô, ... – Pierre Espenan Oct 22, 2010 at 11:47 And How do you use initWithContentsOfURL:usedEncoding:error: ? What is the second parameter ? – Pierre Espenan Oct 22, 2010 at 11:48 Saw that, thanks, but it's still not working (my_string is null). Anyway, I manually replaced the few characters which were not encoded. – Pierre Espenan Oct 22, 2010 at 12:47 of course if your file is not encoded correctly in any encoding supported by the iphone it can't be decoded properly. – Matthias Bauch Oct 22, 2010 at 12:50

Why not use a request and connection to get the info back in an NSData object? Something like this:

NSURL *url = [[NSURL alloc] initWithString:@"http://my_url.com/my_file.xml"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:url];
[request setHTTPMethod:@"GET"];
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self];
    [conn start];
if(conn){
    // Data Received
    responseData = [[NSMutableData alloc] init];

and then in your connection:didRecieveData delegate method, put something like this

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[self.responseData appendData:data];

and then once the connection is finished loading convert the data to a string:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
     NSString *string = [[NSString alloc] initWithData:responseData encoding:NSASCIIStringEncoding];

Not the most straightforward method, but that should get you your XML string. Also if you need to parse the XML once you get it back, you can directly pass the responseData to an NSXMLParser without any conversion. :)

This assumes that the responses' body is in ASCII, which is still the exact same error that the original question has. You must determine encoding type from the response. – Brent Feb 6, 2013 at 0:56

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.