如何同步从联系人获取的iPhone号码与联系人姓名?

我正在创建一个应用程序,我必须在其中获取iPhone的联系人并使用此应用程序调用该应用程序 .

CFErrorRef *error = NULL;
            ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
            CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
            CFIndex numberOfPeople = ABAddressBookGetPersonCount(addressBook);

            for(int i = 0; i < numberOfPeople; i++)
            {
                ABRecordRef person = CFArrayGetValueAtIndex( allPeople, i );

                NSString *firstName = (NSString *)(ABRecordCopyValue(person, kABPersonFirstNameProperty));

                [TempArray addObject:[NSString stringWithFormat:@"%@",firstName]];

                ABMultiValueRef phoneNumbers = ABRecordCopyValue(person, kABPersonPhoneProperty);

                for (CFIndex i = 0; i < ABMultiValueGetCount(phoneNumbers); i++)
                {
                    NSString *phoneNumber = (NSString *) ABMultiValueCopyValueAtIndex(phoneNumbers, i);

                    [PhoneNumbers addObject:phoneNumber];
                }
            }

我得到的数字和名称都在不同的数组中 . 但是有一些联系人名称有多个数字,所以Number Array中的组件更多 .

有没有什么可以同时将数字和联系人同步?有没有什么可以扫描该列表中的手机号码和固定电话号码?

此外,在获取联系人时,我们可以一次获取朋友的完整名称,而无需使用kABPersonFirstNameProperty或kABPersonLastNameProperty并附加?

有什么建议?

回答(1)

2 years ago

我不得不工作一段时间才能完成这项工作 . 我必须使用 ABMultiValueCopyLabelAtIndex 来获得联系,并与 kABPersonPhoneMobileLabelkABPersonPhoneIPhoneLabel 进行比较,并以字典形式获取联系人详细信息 . 在此表单中,数字为 synchronized ,其名称为 .

contactList=[[NSMutableArray alloc] init];
    ABAddressBookRef m_addressbook = ABAddressBookCreate();

    if (!m_addressbook) {
        NSLog(@"opening address book");
    }

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(m_addressbook);
    CFIndex nPeople = ABAddressBookGetPersonCount(m_addressbook);

    for (int i=0;i < nPeople;i++) { 
        NSMutableDictionary *dOfPerson=[NSMutableDictionary dictionary];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople,i);

        //For username and surname
        ABMultiValueRef phones =(NSString*)ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFStringRef firstName, lastName;
        firstName = ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        lastName  = ABRecordCopyValue(ref, kABPersonLastNameProperty);
        [dOfPerson setObject:[NSString stringWithFormat:@"%@ %@", firstName, lastName] forKey:@"name"];

        //For Email ids
        ABMutableMultiValueRef eMail  = ABRecordCopyValue(ref, kABPersonEmailProperty);
        if(ABMultiValueGetCount(eMail) > 0) {
            [dOfPerson setObject:(NSString *)ABMultiValueCopyValueAtIndex(eMail, 0) forKey:@"email"];

        }

        //For Phone number
        NSString* mobileLabel;
        for(CFIndex i = 0; i < ABMultiValueGetCount(phones); i++) {
            mobileLabel = (NSString*)ABMultiValueCopyLabelAtIndex(phones, i);
            if([mobileLabel isEqualToString:(NSString *)kABPersonPhoneMobileLabel])
            {
                [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
            }
            else if ([mobileLabel isEqualToString:(NSString*)kABPersonPhoneIPhoneLabel])
            {
                [dOfPerson setObject:(NSString*)ABMultiValueCopyValueAtIndex(phones, i) forKey:@"Phone"];
                break ;
            }

        [contactList addObject:dOfPerson];
    }
    NSLog(@"array is %@",contactList);
    }