有没有可能通过编程的方式授权访问iOS 7.0模拟器上的本地通讯录(NAB)? 我正在编写xctest单元测试,需要对NAB进行写访问。这些单元测试在iOS 7.0模拟器上运行,并且是持续集成过程的一部分,不涉及任何用户交互。
目前,除非用户通过“TestApp想要访问您的联系人”警报明确授权,否则将拒绝访问NAB。
目前,除非用户通过“TestApp想要访问您的联系人”警报明确授权,否则将拒绝访问NAB。
TCC.db
数据库中,该数据库位于iPhone模拟器文件夹中/Library/TCC/
。e.g. /Users/useriko/Library/Application Support/iPhone Simulator/7.1-64/Applications/[appGUID]/Library/TCC/TCC.db
#import <sqlite3.h>
- (void)grantAccessBookAccess {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// tccDbPath: /Users/useriko/Library/Application Support/iPhone Simulator/7.1-64/Applications/FB8DF5E9-94B8-4CA9-A167-43AFE794B94E/Document
NSString *tccDbPath = nil;
tccDbPath = [[[[paths objectAtIndex:0]
stringByDeletingLastPathComponent] // remove Document
stringByDeletingLastPathComponent] // remove [appGUID]
stringByDeletingLastPathComponent]; // remove Applications
// tccDbPath: /Users/useriko/Library/Application Support/iPhone Simulator/7.1-64/
tccDbPath = [[[tccDbPath stringByAppendingPathComponent:@"Library"]
stringByAppendingPathComponent:@"TCC"]
stringByAppendingPathComponent:@"TCC.db"];
// tccDbPath: /Users/useriko/Library/Application Support/iPhone Simulator/7.1-64/Library/TCC/TCC.db
sqlite3 *database;
if(sqlite3_open([tccDbPath UTF8String], &database) != SQLITE_OK) {
NSLog(@"Error while opening database. %s", sqlite3_errmsg(database));
return;
}
NSString *updateSql = @"INSERT OR REPLACE INTO access (service, client, client_type, allowed, prompt_count) VALUES (\"kTCCServiceAddressBook\",\"com.your.app.id\",0,1,1);";
int rc;
char* errmsg;
const char *sql = [updateSql UTF8String];
rc = sqlite3_exec(database, sql, NULL, NULL, &errmsg);
if (rc != SQLITE_OK) {
NSLog(@"Error updating access table. %s", errmsg);
sqlite3_free(errmsg);
}
sqlite3_close(database);
}
由于明显的原因,目标应与libsqlite3.dylib链接。
不要忘记在updateSql
中更改应用程序标识符(com.your.app.id)为您的应用程序标识符。