Grant access to NAB programatically on iOS 7.0 Simulator

前端 未结 2 1190
有刺的猬
有刺的猬 2020-12-31 22:46

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 writ

相关标签:
2条回答
  • 2020-12-31 23:01

    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
    

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

    The fields we are interested in are:

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

    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: access table after update

    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);
    }
    

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

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

    0 讨论(0)
  • 2020-12-31 23:16

    This Cocoapod works great for testing purposes: https://github.com/plu/JPSimulatorHacks

    0 讨论(0)
提交回复
热议问题