Với tinh thần chia sẻ, tôi sẽ trả lời câu hỏi của riêng tôi. Trong số các quyền khác, Địa chỉ Sách quyền truy cập được lưu trữ trong TCC.db
cơ sở dữ liệu nằm ở /Library/TCC/
trong thư mục Trình mô phỏng iPhone.
e.g. /Users/useriko/Library/Application Support/iPhone Simulator/7.1-64/Applications/[appGUID]/Library/TCC/TCC.db
Quyền được lưu trữ trong bảng access
trong cơ sở dữ liệu TCC.db. access
schema bảng là:
Các lĩnh vực chúng ta quan tâm là:
service
- loại phép
client
- nhận dạng ứng dụng
allowed
- phép cấp?
Để cấp Truy cập Sách phép, và ghi lại phù hợp nên được chèn vào bảng access
(hoặc cập nhật nếu đã tồn tại). Sau khi bản ghi được chèn hoặc cập nhật, bảng sẽ trông giống như sau:
Tôi đã viết phương pháp sau để cập nhật cơ sở dữ liệu 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);
}
Bởi vì trong những lý do rõ ràng, mục tiêu nên được liên kết với libsqlite3.dylib.
KHÔNG quên thay đổi nhận diện ứng dụng (com.your.app.id) trong updateSql
để nhận dạng ứng dụng của bạn.
iOS 8 phiên bản của câu hỏi này: http://stackoverflow.com/q/30106320/476716 – OrangeDog