Downloading a file from url and saving to resources on iPhone

23,317

Solution 1

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!

Solution 2

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"];

Solution 3

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];
Share:
23,317
msk
Author by

msk

Updated on July 26, 2022

Comments

  • msk
    msk almost 2 years

    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!");
        }
    }
    
  • msk
    msk over 14 years
    I want to download the database from the Internet (stored somewhere on some server)
  • msk
    msk over 14 years
    Yes, it is defined that way in the header file.
  • Jordan
    Jordan over 14 years
    I don't see it in viewDidLoad above. Where is it initialized??? fileData = [NSMutableData data];
  • Jordan
    Jordan over 14 years
    Initialize fileData as I stated in my answer (in viewDidLoad) and it'll work.
  • Jordan
    Jordan over 14 years
    You need: fileData = [[NSMutableData alloc] initWithLength:0]; or fileData = [NSMutableData data]; in -(void)viewDidLoad.
  • Jordan
    Jordan over 14 years
    Select this as the answer. Re-Ask your questions with regards to the database.
  • slf
    slf over 14 years
    it doesn't matter, you still need to expand the ~
  • coolcool1994
    coolcool1994 over 7 years
    URL to documents directory is better this way-> NSURL *documentsDirectory = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject]; You can so [URL path] to get the path