在iOS 7.0模拟器上以编程方式授予对NAB的访问权限 [英] Grant access to NAB programatically on iOS 7.0 Simulator

查看:126
本文介绍了在iOS 7.0模拟器上以编程方式授予对NAB的访问权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以通过编程方式在iOS 7.0 Simulator上授予对本机地址簿(NAB)的访问权限?我正在编写 xctest 单元测试,需要写入权限到NAB。单元测试在 iOS 7.0模拟器上运行,是持续集成过程的一部分,不涉及任何用户交互。

Is it possible to grant access to the native address book (NAB) on iOS 7.0 Simulator programmatically? I am writing xctest Unit Tests that need write access to NAB. The unit tests are run on iOS 7.0 Simulator and are part of Continuous Integration process and doesn't involve any user interaction.

目前除非用户通过TestApp明确授予访问权限想要访问您的联系人警报,拒绝访问NAB。

Currently unless the user grants access explicitly via the "TestApp" Would like to Access Your Contacts alert, access to NAB is denied.

推荐答案

本着分享的精神,我将回答我自己的问题。在其他权限中,通讯簿访问权限存储在 / Library / TCC /中的 TCC.db 数据库中在iPhone模拟器文件夹中。

In the spirit of sharing I am going to answer my own question. Among other permissions, Address Book access permission is stored in TCC.db database that is located in /Library/TCC/ in the iPhone Simulator folder.

e.g. /Users/useriko/Library/Application Support/iPhone Simulator/7.1-64/Applications/[appGUID]/Library/TCC/TCC.db

权限存储在TCC.db数据库的 access 表中。 access 表模式是:

Permissions are stored in the access table in TCC.db database. access table schema is:

我们感兴趣的字段是:


  1. 服务 - 权限类型

  2. 客户   - 应用标识符

  3. 允许 - 授予权限?

  1. service - the permission type
  2. client   - the app identifier
  3. allowed - permission granted?

为了授予 Access Book 权限,应在 access 表中插入相应的记录(如果已经存在,则更新) )。在插入或更新记录后,表格应如下所示:

In order to grant Access Book permission, and appropriate record should be inserted into the access table (or updated if already exists). After the record was either inserted or updated the table should look like that:

我编写了以下方法来更新TCC.db数据库。

I've wrote the following method to update the TCC.db database.

#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

Because of the obvious reasons, the target should be linked with libsqlite3.dylib.

不要忘记更改应用标识符( com.your.app.id )在 updateSql 中添加到您的应用标识符。

DO NOT forget to change the app identifier (com.your.app.id) in the updateSql to your app identifier.

这篇关于在iOS 7.0模拟器上以编程方式授予对NAB的访问权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆