6

我正在尝试获取组名称,但经过多次“由用户重新加载联系人”调用此方法后,它会给出nil值和以下错误。

-(void) getGroupsName
    {
        [groupsName removeAllObjects];
        //address book object to interact with iPhone contacts.
        ABAddressBookRef addressbook = ABAddressBookCreate();
        //get groups count
        CFIndex groupsCount          = ABAddressBookGetGroupCount(addressbook);
        //get all available groups as array
        CFArrayRef allGroups         = ABAddressBookCopyArrayOfAllGroups(addressbook);

        for (int i = 0; i<groupsCount; i++) {
            //get group of index=i from groups array
            ABRecordRef group = CFArrayGetValueAtIndex(allGroups, i);
            //get group name, I use __bridge_transfer to transfer from C to objective-c.
            [groupsName addObject:(__bridge_transfer NSString*)ABRecordCopyCompositeName(group)];

        }
        CFRelease(allGroups);
        CFRelease(addressbook);
    }
//////////////////////////////////////////////////////////////

    warning: Could not compile statement PRAGMA journal_mode = wal;: unable to open database file error 14 creating properties table: unable to open database file warning: Could not compile statement SELECT value FROM _SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT value FROM _SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT ROWID, First, Last, Middle, NULL, NULL, NULL, Organization, NULL, NULL, Kind, NULL, NULL, Nickname, Prefix, Suffix, FirstSort, LastSort, CreationDate, ModificationDate, CompositeNameFallback, NULL, StoreID, NULL, FirstSortSection, LastSortSection, FirstSortLanguageIndex, LastSortLanguageIndex, NULL, NULL, NULL, PersonLink, NULL, IsPreferredName FROM ABPerson;: unable to open database file warning: Could not compile statement SELECT ROWID, First, Last, Middle, NULL, NULL, NULL, Organization, NULL, NULL, Kind, NULL, NULL, Nickname, Prefix, Suffix, FirstSort, LastSort, CreationDate, ModificationDate, CompositeNameFallback, NULL, StoreID, NULL, FirstSortSection, LastSortSection, FirstSortLanguageIndex, LastSortLanguageIndex, NULL, NULL, NULL, PersonLink, NULL, IsPreferredName FROM ABPerson;: unable to open database file warning: Could not compile statement INSERT OR REPLACE INTO _SqliteDatabaseProperties VALUES (?, ?);: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement INSERT OR REPLACE INTO
_SqliteDatabaseProperties VALUES (?, ?);: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT value FROM
_SqliteDatabaseProperties WHERE key = ?;: unable to open database file warning: Could not compile statement SELECT ROWID FROM ABGroup;: unable to open database file warning: Could not compile statement SELECT ROWID, Name, ExternalIdentifier, StoreID, NULL, NULL, NULL FROM ABGroup;: unable to open database file

所以我使用本机通知让我知道什么时候addressbook被修改以减少我访问的次数addressbook,但如果用户进行多次更新并且每次addrssbook修改必须调用这个肉类或任何其他相关的addressbook.

所以还需要你的帮助???

4

3 回答 3

1

ABAddressBookCreate已在 iOS6 中弃用。使用,如果您无权访问通讯录ABAddressBookCreateWithOptions,它将返回一个对象。CFErrorRef

于 2012-12-23T14:20:32.553 回答
0

您为什么不尝试ABAddressBookRef使用以下方法尽量减少创建和访问的时间:

void ABAddressBookRegisterExternalChangeCallback (
   ABAddressBookRef addressBook,
   ABExternalChangeCallback callback,
   void *context
);

这允许您的应用程序知道地址簿何时被外部修改。并让您知道您需要重新访问地址簿。

您发布的方法与您发布的方法相同,每次调用它都会创建具有新访问地址簿的新对象

于 2012-12-30T12:25:22.120 回答
-1

正如@thelaws 所指出的,您必须在IOS6 中使用ABAddressBookCreateWithOptions。if ([[[UIDevice currentDevice] systemVersion] floatValue] > 5.1) {

   ABAddressBookRef ab = ABAddressBookCreateWithOptions(NULL, NULL);
    __block BOOL accessGranted = NO;
    if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined) {
        ABAddressBookRequestAccessWithCompletion(ab, ^(bool granted, CFErrorRef error) {
            // First time access has been granted, add the contact
            accessGranted = granted;
        });
    }
    else if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusAuthorized)        {
        // The user has previously given access, add the contact
        accessGranted = YES;
    }
    else {
        // The user has previously denied access
        // Send an alert telling user to change privacy setting in settings app
    }
    if (accessGranted) {
// your code goes here
 }
}
else {
// your code goes here
}

另一件事是您是否已在某处初始化了 groupsName ?

除此之外,您是否使用 ABExternalChangeCallBack 来获取地址簿更改的通知?

我认为您应该将组代码放在访问联系人的同一模块中。(如果这对您没有帮助,请澄清!)

于 2012-12-25T13:58:49.120 回答