How to use stringWithContentsOfURL:encoding:error:

I am trying to use initWithContentsOfURL:encoding:error: like this :


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 :)

share|improve this question
 
 
have you checked the error code? –  Nick Moore Oct 22 '10 at 10:02
 
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 '10 at 10:34 
add comment

3 Answers

up vote20down voteaccepted

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.

share|improve this answer
 
 
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 '10 at 11:47
 
And How do you use initWithContentsOfURL:usedEncoding:error: ? What is the second parameter ? – Pierre Espenan Oct 22 '10 at 11:48 
 
@Nonepse I made an edit. HTH –  Matthias Bauch Oct 22 '10 at 12:29
 
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 '10 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 '10 at 12:50
show 2 more comments

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. :)

share|improve this answer
 
 
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 '13 at 0:56
add comment

You can modify your webpage by adding header('Content-Type: text/html; charset=UTF-8'); to the top of the code and saving the document as a UTF-8 formated file. It helped me as I had the same problem.

share|improve this answer

轉自http://stackoverflow.com/questions/3995519/how-to-use-stringwithcontentsofurlencodingerror/3995954#3995954
發佈了1 篇原創文章 · 獲贊 0 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章