How to download CSV file from server in Objective-C

前端 未结 2 1036
天涯浪人
天涯浪人 2020-12-11 07:13

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 prog

2条回答
  •  粉色の甜心
    2020-12-11 07:59

    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.

提交回复
热议问题