André
André

Reputation: 105

Download sqlite database from Dropbox

I am trying to download an sqlite database from my Dropbox using a direct link as the source path, and then saving it to the app's document file. It's just a means of updating the database without having to update the app.

When I check if the file exists before downloading, the file is not visible.

NSFileManager *fileManager = [NSFileManager defaultManager];

NSString *sourceDBPath = @"https://www.dropbox.com/s/868vvg7uremst9n/Data.sqlite?dl=1";

if ([fileManager fileExistsAtPath:sourceDBPath])
{

NSData *dbFile = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:@"https://www.dropbox.com/s/868vvg7uremst9n/Data.sqlite?dl=1"]];

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

NSString *filePath = [resourceDocPath stringByAppendingPathComponent:@"Database.sqlite"];


[dbFile writeToFile:filePath atomically:YES];
}

Would I need to integrate the Dropbox SDK into my app even if I am using a direct link to the file?

Upvotes: 2

Views: 829

Answers (1)

ocodo
ocodo

Reputation: 30289

Well, the link is definitely working, and no, you don't need to link up Dropbox and your app for this to work.

There are some issues with https and ios detailed here:

iOS 5's TLS implementation has been upgraded to support TLS protocol version 1.2. Some non-compliant TLS server implementations do not implement TLS 1.2 and, more importantly, do not downgrade gracefully to a supported protocol version. As a result you may find that some TLS client applications built against the iOS 5 SDK may not connect to some TLS servers, when the same application built against a previous version of the iOS SDK would connect.

Excerpted from...

Update.

However, I just tried to retrieve it using the following code:

NSString *dropboxHttpsUrl = @"https://www.dropbox.com/s/868vvg7uremst9n/Data.sqlite?dl=1";
NSData *dbFile = [[NSData alloc] initWithContentsOfURL:
                      [NSURL URLWithString:dropboxHttpsUrl]];

if(dbFile) {
   NSLog(@"%i", [dbFile length]);
   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
   NSString *documentsDirectory = [paths objectAtIndex:0];
   NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"test.db"];
   [dbFile writeToFile:appFile atomically:YES];
   NSLog(@"%@", appFile);
}else{
   NSLog(@"nothing got retrieved");
}

And I was able to grab the database and see the empty Example table and schema.

Just build you document path in a more standard fashion:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appFile = [documentsDirectory stringByAppendingPathComponent:@"test.db"];

Don't do:

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

It's this that's probably saving it to the wrong place, or worse not at all.

Upvotes: 1

Related Questions