Search By Number and Get the image using ABAddressBook

若如初见. 提交于 2019-11-30 00:53:01

The AB framework can be a real pain at times. But it breaks down to a series of pretty simple operations. First, you have to create an ABAddressBook instance:

ABAddressBookRef addressbook = ABAddressBookCreate();

Then you'll want to make a copy of the array of all people in the address book, and step through them looking for the data you want:

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressbook);
CFIndex numPeople = ABAddressBookGetPersonCount(addressbook);
for (int i=0; i < numPeople; i++) { 

Inside your loop, you'll probably want to get a reference to the individual person:

ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);

Then you want to compare the number you have (lets call that inNumber) to every phone number associated with that particular person. To do that, you first need a list of all the person's phone numbers:

ABMutableMultiValueRef phonelist = ABRecordCopyValue(person, kABPersonPhoneProperty);

Then, of course, you'll need to have an inner loop that loops over each of the individual person's phone numbers:

CFIndex numPhones = ABMultiValueGetCount(phones);
for (int j=0; j < numPhones; j++) {

Since the phone numbers have both numbers and labels associated with them, you'll need to extract the actual phone number string as an NSString:

CFTypeRef ABphone = ABMultiValueCopyValueAtIndex(phoneList, j);
NSString *personPhone = (NSString *)ABphone;
CFRelease(ABphone);

Now you can finally compare numbers! Do so with the standard NSString comparison methods, but remember that you need to worry about formatting, etc.

Once you find the person who has a phone number matching inNumber, you'll want the extract that person's image into a UIImage:

    CFDataRef imageData = ABPersonCopyImageData(person);
    UIImage *image = [UIImage imageWithData:(NSData *)imageData];
    CFRelease(imageData);

When it comes time to exit, you'll need to clean up memory. A general rule of thumb for the AB framework is that anything with Get in the function name you don't need to release, and anything with Copy or Create, you do need to release. So, in this case you'll need to CFRelease() phonelist, allPeople, and addressbook, but not numPeople, person, or numPhones.

-(void)fetchAddressBook:(NSString *)searchnumber
{
    ABAddressBookRef UsersAddressBook = ABAddressBookCreateWithOptions(NULL, NULL);

    //contains details for all the contacts
    CFArrayRef ContactInfoArray = ABAddressBookCopyArrayOfAllPeople(UsersAddressBook);

    //get the total number of count of the users contact
    CFIndex numberofPeople = CFArrayGetCount(ContactInfoArray);

    //iterate through each record and add the value in the array
    for (int i =0; i<numberofPeople; i++) {

        ABRecordRef ref = CFArrayGetValueAtIndex(ContactInfoArray, i);

        NSString *firstName = (__bridge NSString *)ABRecordCopyValue(ref, kABPersonFirstNameProperty);
        //Get phone no. from contacts
        ABMultiValueRef multi = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        UIImage *iimage;
        NSString* phone;
        for (CFIndex j=0; j < ABMultiValueGetCount(multi); j++) {
            iimage=nil;
            phone=nil;
            phone = (__bridge NSString*)ABMultiValueCopyValueAtIndex(multi, j);



            //if number matches
            if([phone isEqualToString:searchnumber])
            {
                NSLog(@"equlas%@",searchnumber);

            //if person has image store it
            if (ABPersonHasImageData(ref)) {

                CFDataRef imageData=ABPersonCopyImageDataWithFormat(ref, kABPersonImageFormatThumbnail);
                iimage = [UIImage imageWithData:(__bridge NSData *)imageData];

            }else{
                //default image
                iimage=[UIImage imageNamed:@"icon"];

            }

         //set image and name
                userimage.image=iimage;
                lblname.text=firstName;

                return;
            }


        }

    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!