iOS 7 以降および OS X 10.9 以降の場合:
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];
以前のバージョンの場合:
[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);
}
}
}];
より簡単に実装できるソリューションをお探しの場合は、このリンクをご覧ください