ABAddressBookCreate() , ABAddressBookGetGroupCount , ... return @"0x00000000 <nil>"?
Asked Answered
W

3

6

I'm trying to get Groups Name but after many time call this method "by the user to reload contacts" it give the nil value and the following error.

-(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

So I use the native notification to let me know when addressbook get modified to decrease number of time I access the addressbook, but still not good by the time if user make many update and every-time addrssbook get modified must call this meathod or any other one related to addressbook.

so still need your help ???

Westnorthwest answered 23/12, 2012 at 10:44 Comment(3)
Please see #13054476Kellner
@Kellner it is not the same case, my code get all contact at the first time and every time but , i have reload button when user click it more than 20 time it return the error aboveWestnorthwest
Sorry, I didn't realize you only had the problem after many iterations.Kellner
A
0

Why are you not tring to minimize number of time you create and access the ABAddressBookRef by using the following :

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

this allow to your app know when the addressBook get modify by external. and to let you know that you need to re-access the address-book.

The same thing for your method that you post it, every time you call it will create new object with new access to address book

Autoxidation answered 30/12, 2012 at 12:25 Comment(1)
thx it is work for me, for now no crash and no SQL warring :)Westnorthwest
A
1

ABAddressBookCreate has been decprecated in iOS6. Use ABAddressBookCreateWithOptions, it will return a CFErrorRef object if you don't have access to the address book.

Admiral answered 23/12, 2012 at 14:20 Comment(3)
still give me the same problem after call the method many time it will return null so i think that from the sdk it self ??Westnorthwest
Did you try looking at the CFErrorRef?Anglophile
like you see it is warning not an error so can't Log it or do any thin for &errorWestnorthwest
A
0

Why are you not tring to minimize number of time you create and access the ABAddressBookRef by using the following :

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

this allow to your app know when the addressBook get modify by external. and to let you know that you need to re-access the address-book.

The same thing for your method that you post it, every time you call it will create new object with new access to address book

Autoxidation answered 30/12, 2012 at 12:25 Comment(1)
thx it is work for me, for now no crash and no SQL warring :)Westnorthwest
M
-1

As pointed out by, @thelaws you have to use ABAddressBookCreateWithOptions in IOS6. 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
}

Another thing is have you initialized, groupsName somewhere ?..

Apart from it, are you using ABExternalChangeCallBack for getting Notification of addressBook change?

I think you should put Group code within the same module, where you are accessing contacts. (If this doesn't help you, do clarify!)

Midas answered 25/12, 2012 at 13:58 Comment(2)
first thing yeas i use ABExternalChangeCallBack and this give me more 10 time to avoid crash "when user update then the app get notify. to re-access address book if this happened multi. time". Second: it is the same for get contacts if i ask for it multi time it will be crash or return to me nil value. so this doesn't help :(Westnorthwest
Correct in principle, but -1 for taking the floatValue of the system version. That is NOT the recommended way to check for API availability.Mcwilliams

© 2022 - 2024 — McMap. All rights reserved.