5

Addressbook在我的 IOS 应用程序中使用来获取联系人姓名和号码,但是发生了一件奇怪的事情,它显示了一些联系人的电话号码。

我希望它会显示所有带有电话号码的联系人列表。

所以这是我获取电话号码的代码:

-(void)getAddressbookData
{
    #if __IPHONE_OS_VERSION_MIN_REQUIRED < 60000
        ABAddressBookRef addressBook = ABAddressBookCreate();
    #else
        CFErrorRef *error = nil;
        ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
    #endif
        NSArray * people;
        BOOL accessGranted = [self __addressBookAccessStatus:addressBook];

        if (accessGranted)
        {
            people = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
            // Do whatever you need with thePeople...
        }
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray *contactArray = [[NSMutableArray alloc] init];

        for (CFIndex i = 0; i < nPeople; i++)
        {
            ABRecordRef record = CFArrayGetValueAtIndex((__bridge CFArrayRef)(people), i);
            NSString *firstName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonFirstNameProperty);
            NSString *lastName = (__bridge NSString *)ABRecordCopyValue(record, kABPersonLastNameProperty);
            NSString *fullName = nil;

            if (ABPersonGetCompositeNameFormat() == kABPersonCompositeNameFormatFirstNameFirst)
                fullName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
            else
                fullName = [NSString stringWithFormat:@"%@, %@", lastName, firstName];

            [contactArray addObject:fullName];

            //
            // Phone Numbers
            //
            ABMutableMultiValueRef phoneNumbers = ABRecordCopyValue(record, kABPersonPhoneProperty);
            CFIndex phoneNumberCount = ABMultiValueGetCount( phoneNumbers );

            NSMutableArray *numbersArray = [[NSMutableArray alloc] init];
            for ( CFIndex k=0; k<phoneNumberCount; k++ )
            {
                CFStringRef phoneNumberLabel = ABMultiValueCopyLabelAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberValue = ABMultiValueCopyValueAtIndex( phoneNumbers, k );
                CFStringRef phoneNumberLocalizedLabel = ABAddressBookCopyLocalizedLabel( phoneNumberLabel );
                // converts "_$!<Work>!$_" to "work" and "_$!<Mobile>!$_" to "mobile"

                // Find the ones you want here
                //
               // NSLog(@"-----PHONE ENTRY -> name:%@ :  %@ : %@", fullName, phoneNumberLocalizedLabel, phoneNumberValue );
                [numbersArray addObject:CFBridgingRelease(phoneNumberValue)];

                CFRelease(phoneNumberLocalizedLabel);
                CFRelease(phoneNumberLabel);
                CFRelease(phoneNumberValue);
            }

           // NSLog(@"phone numbers %@", numbersArray);
            [contactDictionary setObject:numbersArray forKey:fullName];

            CFRelease(record);

        }

        selectContacts = contactArray;
       // NSLog(@"dictionary of array %@", contactDictionary);

        //NSLog(@"contacts count %d", [selectContacts count]);
    }

    -(BOOL)__addressBookAccessStatus:(ABAddressBookRef) addressBook
    {
        __block BOOL accessGranted = NO;

        if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
            dispatch_semaphore_t sema = dispatch_semaphore_create(0);

            ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
                accessGranted = granted;
                dispatch_semaphore_signal(sema);
            });

            dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
            // dispatch_release(sema);
        }
        else { // we're on iOS 5 or older
            accessGranted = YES;
        }
        return accessGranted;
    }

所以对于一些联系人来说 numbersArray 是空白的,我不知道为什么会这样。

4

3 回答 3

5

试试这个电话号码。

    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *people = (NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
    for(id person in people){
    //fetch multiple phone nos.
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
            NSString* phone = (NSString*)ABMultiValueCopyValueAtIndex(multi, j);
            [numbersArray addObject:phone];
            [phone release];
        }
    }

你必须在使用前分配你的数组。在 viewDidLoad 方法中为 alloc 数组写这个

           numbersArray=[[NSMutableArray alloc] init];
于 2013-05-17T12:07:02.250 回答
1

我正在使用此代码从我的通讯录中访问手机号码,并且工作正常。

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef allSources = ABAddressBookCopyArrayOfAllPeople( addressBook );
for (CFIndex i = 0; i < ABAddressBookGetPersonCount( addressBook ); i++)
    {
ABRecordRef aSource = CFArrayGetValueAtIndex(allSources,i);    
ABMultiValueRef phones =(NSString*)ABRecordCopyValue(aSource, kABPersonPhoneProperty);
        NSString* mobileLabel;

        for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {

            mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);

            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            {
                home_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i) retain];

            }

            if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
            {
                basic_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i)retain];


            }
            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMainLabel])
            {
                work_mobile = [(NSString*)ABMultiValueCopyValueAtIndex(phones, i)retain];

            }
        }

你可以试试这个。

于 2013-05-17T12:14:46.283 回答
1

这是对我有用的@Samir 答案的Swift 版本。

var allNumbers: [AnyObject] = []
let adbk : ABAddressBook? = ABAddressBookCreateWithOptions(nil, nil).takeRetainedValue()
let people = ABAddressBookCopyArrayOfAllPeople(adbk).takeRetainedValue() as NSArray as [ABRecord]
for person in people {
    var phones: ABMultiValueRef = ABRecordCopyValue(person, kABPersonPhoneProperty).takeRetainedValue()
    for j in 0..<ABMultiValueGetCount(phones) {
        var phone: String = ABMultiValueCopyValueAtIndex(phones, j).takeRetainedValue() as String
        allNumbers.append(phone)
    }
}
println(allNumbers)
于 2014-11-04T01:53:34.530 回答