2

我正在尝试使用直接链接作为源路径从我的 Dropbox 下载 sqlite 数据库,然后将其保存到应用程序的文档文件中。这只是一种无需更新应用程序即可更新数据库的方法。

当我在下载前检查文件是否存在时,该文件不可见。

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

即使我使用文件的直接链接,我是否需要将 Dropbox SDK 集成到我的应用程序中?

4

1 回答 1

1

好吧,这个链接肯定是有效的,不,你不需要链接 Dropbox 和你的应用程序就可以了。

这里详细介绍了 https 和 ios 的一些问题:

iOS 5 的 TLS 实现已升级为支持 TLS 协议版本 1.2。一些不兼容的 TLS 服务器实现不实现 TLS 1.2,更重要的是,不会优雅地降级到支持的协议版本。因此,您可能会发现某些针对 iOS 5 SDK 构建的 TLS 客户端应用程序可能无法连接到某些 TLS 服务器,而针对先前版本的 iOS SDK 构建的相同应用程序会连接。

摘自...

更新。

但是,我只是尝试使用以下代码检索它:

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");
}

我能够抓取数据库并查看空Example表和架构。

只需以更标准的方式构建您的文档路径:

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

不要这样做:

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

正是这个可能将它保存到错误的地方,或者更糟糕的是根本没有。

于 2012-12-22T06:14:42.730 回答