0

Possible Duplicate:
How to download CSV file from server in Objective-C

How can I get my app to download a CSV file from a webpage? Specifically I'm trying to get the one here:

http://www.google.com/trends/explore#cat=0-14&date=today%207-d&cmpt=q

(settings>download CSV)

Community
  • 1
  • 1
  • Your question matches an existing SO question. Follow this SO post to understand how to download CSV file from a server in iOS: http://stackoverflow.com/questions/10383341/how-to-download-csv-file-from-server-in-objective-c – Balaji Kandasamy Jan 26 '13 at 11:09
  • Do you know how to download a file using an URL connection? Then you know how to download that file. The fact that it's a CSV file doesn't matter. – David Rönnqvist Jan 26 '13 at 13:27

1 Answers1

4

Firstly you need to identify the URL of the resource you wish to download over HTTP. After having a look at the HTML I found that this is the actual URL for the CSV:

http://www.google.com/trends/trendsReport?hl=en-US&cat=0-14&date=today%207-d&cmpt=q&content=1&export=1

You will need to experiment and see how this is formatted.

Now that you have the URL you need to download it using HTTP. You can either use the SDK's request system or a far superior library such as ASIHTTPRequest or AFNetworking. I would use AFNetworking since the former has now been discontinued by its developer.

https://github.com/AFNetworking/AFNetworking

NSURL *url = [NSURL URLWithString:@"http://domain.com/theFileYouWant.csv"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]initWithRequest:request];
[operation  setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

    NSLog(@"success: %@", operation.responseString);

} 
    failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"error: %@",  operation.responseString);

}
 ];
[operation start];

This is some sample code to download a CSV file with AFNetworking (I haven't tested this). Once you have downloaded the file you can perform whatever operations you wanted to on it.

Hope this helps!

Alex Stuckey
  • 1,250
  • 1
  • 14
  • 28