ani
ani

Reputation: 63

how can get the contact image of person from address book by passing the mobile number as an argument

how can we get the contact image of person from address book by passing the mobile number as an argument. I don't want to open the address book.

I have one Image Well and other-one text field with number and one button for getting the image.

I just want to put 10 digit number into text field. and press the button then I want to get the thumbnail image of the person from the address book that have this number. I just want to get the thumbnail image of person from mobile number that i m passing from my UIView.but i don't want go open the address book and select any contact from list into my UIView page.

please any one help me for the code. i am new in iphone development.

Upvotes: 2

Views: 417

Answers (1)

Sumanth
Sumanth

Reputation: 4921

Check the sample for getting the image from phone number

NSString phoneNumber = @"yourPhoneNumber";
UIImage *myContactImage;
ABAddressBookRef addressBook = ABAddressBookCreate();

    // Get all contacts in the addressbook
    NSArray *allPeople = (__bridge NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

    for (id person in allPeople) {
        // Get all phone numbers of a contact
        ABMultiValueRef phoneNumbers = ABRecordCopyValue((__bridge ABRecordRef)(person), kABPersonPhoneProperty);

        // If the contact has multiple phone numbers, iterate on each of them
        for (int i = 0; i < ABMultiValueGetCount(phoneNumbers); i++) {
            NSString *phone = (__bridge_transfer NSString*)ABMultiValueCopyValueAtIndex(phoneNumbers, i);

            // Remove all formatting symbols that might be in both phone number being compared
            NSCharacterSet *toExclude = [NSCharacterSet characterSetWithCharactersInString:@"/.()- "];
            phone = [[phone componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString: @""];
            phoneNumber = [[phoneNumber componentsSeparatedByCharactersInSet:toExclude] componentsJoinedByString: @""];

            if ([phone isEqualToString:phoneNumber]) {
                NSData *contactImageData = (NSData*)ABPersonCopyImageData(person);
                myContactImage = [[UIImage alloc] initWithData:contactImageData];
                break;
                break;
            }
        }
    }

Upvotes: 2

Related Questions