我正忙着为我的应用创建iOS 8 Today扩展程序.我想从我的扩展程序访问我的SQLite(而非Core Data)数据库.在网上搜索一下后,我发现我需要一个App Group.所以我为我的应用程序创建了一个名为"group.AppName"的应用程序组.
使用以下代码,我在NSDocumentDirectory中创建一个SQLite数据库.但App Extensions无法访问NSDocumentDirectory.这就是我想在我的应用程序组中保存SQLite数据库的原因.
NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [path objectAtIndex:0];
dbPathString = [docPath stringByAppendingPathComponent: @"dbName.db"];
char *error;
NSFileManager *fileManager = [NSFileManager defaultManager];
if(![fileManager fileExistsAtPath:dbPathString])
{
const char *dbPath = [dbPathString UTF8String];
if(sqlite3_open(dbPath, &treinAppDB) == SQLITE_OK)
{
const char *sql_stat = "CREATE TABLE IF NOT EXISTS table (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, code TEXT, lat TEXT, lon TEXT)";
sqlite3_exec(appDB, sql_stat, NULL, NULL, &error);
sqlite3_close(appDB);
}
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚如何在我的应用程序组中创建此数据库,因此我可以从我的今日扩展程序访问该数据库.
任何人都可以帮我解决这个问题吗?那太好了!