2

Basically I'd like to build a simple currency converter that fetches data dynamically from the web (Atm best I've come up with is: http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml , if you know any better that has a JSON result, I'd appreciate it).

Now I noticed it doesn't have the XML format like I've seen in some tutorials, so I thought about getting everything from the URL as string and parsing it as a string (I'm pretty good with string parsing, done a lot at C++ contests).

My question is, how do I get the string from the URL?

URL: http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml

2
  • Could you post a sample of the URL you are talking about? Commented Jul 24, 2013 at 15:57
  • It sure looks like XML to me. Commented Jul 24, 2013 at 16:00

1 Answer 1

7

For iOS 7+ and OS X 10.9+ use:

NSURLSession *aSession = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
[[aSession dataTaskWithURL:[NSURL URLWithString:@"http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    if (((NSHTTPURLResponse *)response).statusCode == 200) {
        if (data) {
            NSString *contentOfURL = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", contentOfURL);
        }
    }
}] resume];

For earlier versions use:

[NSURLConnection sendAsynchronousRequest:[[NSURLRequest alloc] initWithURL:[NSURL URLWithString:@"http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml"]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
    if (((NSHTTPURLResponse *)response).statusCode == 200) {
        if (data) {
            NSString *contentOfURL = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            NSLog(@"%@", contentOfURL);
        }
    }
}];

If you are looking for an easier to implement solution take a look at this link

Sign up to request clarification or add additional context in comments.

4 Comments

That's the simple solution and it will work in this case, but it has two problems: 1) it's synchronous so if the server is slow to respond, your app will appear to hang until it does. 2) in the general case, you can't just assume the response will be in UTF-8. You need to be more clever about character encodings.
@JeremyP Yes, I totally agree with your first point, you never should do that in shipping code. Your second point, do you mean even if the encoding in the XML says "UTF-8" it can change, or do you mean that they can change the encoding? Then you would be better of using stringWithContentsOfURL:usedEncoding:error:
Well you can always do it in an AsyncronousRequest's completion block, parse it, then update the data when it's done, can't you?
@HAS If the XML says charset="utf-8" then the text is meant to be in UTF-8 clearly and if it isn't you can assume a server side bug. With XML, I would sidestep the issue altogether, get the body as an NSData and pass it directly to an XML parser.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.