4

I'm developing a new iPhone application, Here i have parsed a 'csv' file from local, and its working with me. When i try to download the 'csv' file from the server programmatically, it didn't workout for me. Could you please help me?

Loading data from local file (Working fine)

- (void)viewDidLoad
{  
    [super viewDidLoad];

    NSString * file = [[NSBundle bundleForClass:[self class]] pathForResource:@"sample" ofType:@"csv"];

    NSStringEncoding encoding = 0;
    NSString * csv = [NSString stringWithContentsOfFile:file usedEncoding:&encoding error:nil];
    NSArray *fields = [csv CSVComponents];
    NSLog(@"fields: %@", fields); //getting the result content 
}

Download the file from Server (failed)

-(void) connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"connectionDidFinishLoading"); //nothing showing here

    NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fullName = [NSString stringWithFormat:@"quotes.csv"];

    NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@",docDir,fullName];
    [receivedData writeToFile:fullFilePath atomically:YES];
} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    NSLog(@"data: %@", data); //nothing showing here

    if (receivedData)
        [receivedData appendData:data];
    else 
        receivedData = [[NSMutableData alloc] initWithData:data];
}

- (void)loadDatafromURL
{    
    NSURL *url = [NSURL URLWithString:@"http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC,^IXIC,^dji,^GSPC,^BVSP,^GSPTSE,^FTSE,^GDAXI,^FCHI,^STOXX50E,^AEX,^IBEX,^SSMI,^N225,^AXJO,^HSI,^NSEI&f=sl1d1t1c1ohgv&e=.csv"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [NSURLConnection connectionWithRequest:request delegate:self];
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
shebi
  • 707
  • 2
  • 14
  • 23

2 Answers2

5

Implement this method:

-(void)connection:(NSURLConnection *)conn didFailWithError:(NSError *)error

You'll find that you're getting an error of

Error Domain=NSURLErrorDomain Code=-1000 "bad URL" UserInfo=0xf663f40 {NSUnderlyingError=0xf663de0 "bad URL", NSLocalizedDescription=bad URL}

I've looked into downloading information this way before, and I think one problem you're having is that separate symbols have to be separated with a "+". Also, when pulling an index, you can't pass the "^" symbol as part of the URL. You have to replace it with "%5E".

So, add this:

- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"%@", [error description]);
}

And change your URL to this:

NSString *urlString = @"http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC+^IXIC+^dji+^GSPC+^BVSP+^GSPTSE+^FTSE+^GDAXI+^FCHI+^STOXX50E+^AEX+^IBEX+^SSMI+^N225+^AXJO+^HSI+^NSEI&f=sl1d1t1c1ohgv&e=.csv";
NSURL *url = [NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];

Now it works for me! I even checked the output .csv file, and it looks good to go! One full quote per line.

mbm29414
  • 11,558
  • 6
  • 56
  • 87
0

If you plan on fetching more data over the network than this single csv, you could have a look at AFNetworking, it's a great library for doing network operations.

A working solution would then look a bit like this:

- (void)getCSVAsynch {
    NSString *unescaped = @"http://download.finance.yahoo.com/d/quotes.csv?s=^GSPC,^IXIC,^dji,^GSPC,^BVSP,^GSPTSE,^FTSE,^GDAXI,^FCHI,^STOXX50E,^AEX,^IBEX,^SSMI,^N225,^AXJO,^HSI,^NSEI&f=sl1d1t1c1ohgv&e=.csv";
    NSURL *url = [NSURL URLWithString:[unescaped stringByAddingPercentEscapesUsingEncoding:NSASCIIStringEncoding]];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"CSV: %@", [[NSString alloc] initWithBytes:[responseObject bytes] length:[responseObject length] encoding:NSUTF8StringEncoding]);
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Things go boom. %@", [error localizedDescription]);
    }];
    [operation start];
}
Bart Vandendriessche
  • 1,372
  • 13
  • 14
  • This is overkill, since the built-in NSURLConnection does *exactly* what he wants with little overhead. You don't even have to import any headers! – mbm29414 Apr 30 '12 at 12:32
  • If this is the only networking code he'll have, I agree with you. If his app is going to pull in more data over the network, looking at AFNetworking may very well be worth it. – Bart Vandendriessche Apr 30 '12 at 12:35
  • Fair enough. I tend to get tunnel vision and just focus on the question at hand. :-) – mbm29414 Apr 30 '12 at 12:38