我正在尝试使用直接链接作为源路径从 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 集成到我的应用中?
最佳答案
嗯,该链接肯定有效,而且不,您不需要链接 Dropbox 和您的应用程序即可正常工作。
https 和 ios 存在一些问题,详细信息如下:
iOS 5 的 TLS 实现已升级,支持 TLS 协议(protocol)版本 1.2。一些不合规的 TLS 服务器实现不实现 TLS 1.2,更重要的是,不会正常降级到受支持的协议(protocol)版本。因此,您可能会发现,针对 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"]];
这可能会将其保存到错误的位置,或者更糟糕的是根本不保存。
关于iphone - 从 Dropbox 下载 sqlite 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13999497/