Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downloading a file from url and saving to resources on iPhone

Is it possible to download a file (i.e. an sqlite database file) from the Internet into your iPhone application programmatically for later use within the application?

I am trying using NSURLConnection, but not able to save the file.

Here is the example code I am trying:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *file = [NSString stringWithFormat:@"http://en.wikipedia.org/wiki/Text_file"];
    NSURL *fileURL = [NSURL URLWithString:file];

    NSURLRequest *req = [NSURLRequest requestWithURL:fileURL];
    NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];  
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [self.fileData setLength:0];
    self.totalFileSize = [NSNumber numberWithLongLong:[response expectedContentLength]];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
        [self.fileData appendData:data];        
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    NSUserDomainMask, YES);
    NSLog(@"%@", [dirArray objectAtIndex:0]);

    NSString *path = [NSString stringWithFormat:@"%@/blah.text", [dirArray objectAtIndex:0]];

    if ([self.fileData writeToFile:path options:NSAtomicWrite error:nil] == NO) {
        NSLog(@"writeToFile error");
    }
    else {
        NSLog(@"Written!");
    }
}
like image 837
msk Avatar asked Jan 20 '10 15:01

msk


People also ask

How do I download a file with a download URL?

Most files: Click on the download link. Or, right-click on the file and choose Save as. Images: Right-click on the image and choose Save Image As. Videos: Point to the video.


3 Answers

What is fileData? How is it defined and initialized?

  fileData = [NSMutableData data];

should be somewhere after:

NSURLConnection *conn = [NSURLConnection connectionWithRequest:req delegate:self];

You have to initialize fileData and retain it per memory management rules.

Try it with my answer. It Works!

like image 93
Jordan Avatar answered Oct 20 '22 16:10

Jordan


You need to expand the path with stringByAppendingPathComponent

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"bookdb.sql"];
like image 34
slf Avatar answered Oct 20 '22 15:10

slf


Try this code in a method and execute,

NSURL *url=[NSURL URLWithString:@"http://en.wikipedia.org/wiki"];

NSData *dbFile = [[NSData alloc] initWithContentsOfURL:url];   

NSString *resourceDocPath = [[NSString alloc] initWithString:[[[[NSBundle mainBundle]  resourcePath] stringByDeletingLastPathComponent]stringByAppendingPathComponent:@"Documents"]];

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Text_file.txt"];  

[dbFile writeToFile:filePath atomically:YES];
like image 30
SURESH SANKE Avatar answered Oct 20 '22 16:10

SURESH SANKE