0

Guys am working on a calling app for Ios,so naturally i have a dial-pad integrated. The problem am facing in to get the contact details from number dialed from my dial-pad which i am showing in other screen.

Here is my code

+(PhoneContactModel*) getContactFrom:(NSString *)calledPhoneNumber{

    PhoneContactModel *contact=[[PhoneContactModel alloc]init];

    ABAddressBookRef addressBook = [AppUtils getCompatibleAdressBook];
    CFArrayRef all = ABAddressBookCopyArrayOfAllPeople(addressBook);
    CFIndex n = ABAddressBookGetPersonCount(addressBook);

    for( int i = 0 ; i < n ; i++ )
    {
        ABRecordRef ref = CFArrayGetValueAtIndex(all, i);

        ABMultiValueRef phones = (ABMultiValueRef)ABRecordCopyValue(ref, kABPersonPhoneProperty);


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

            CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(phones, j);
            //CFRelease(phones);
            NSString *phoneNumber = (__bridge NSString *)phoneNumberRef;

            NSLog(@"apputil number %@",[AppUtils getNumberSanatized:phoneNumber]);

            if ([phoneNumber isEqualToString:calledPhoneNumber]){

                NSLog(@"apputil number matched %@",[AppUtils getNumberSanatized:phoneNumber]);
                contact.strFullName = (__bridge_transfer NSString *) ABRecordCopyValue(ref, kABPersonFirstNameProperty);
                NSString *lastName = (__bridge_transfer NSString *) ABRecordCopyValue(ref, kABPersonLastNameProperty);
                contact.strFullName=[contact.strFullName stringByAppendingString:@" "];

                 if (lastName!=nil){
                contact.strFullName=[contact.strFullName stringByAppendingString:lastName];
                 }
                contact.imgContactImge=[AppUtils imageForContact:ref];
                contact.strNumber=phoneNumber;


                return contact;
            }

        }
    }

    contact.strFullName = calledPhoneNumber;
    return contact;
}

The problem appears like if i have a Contact A with number 64xxxx... and i dial +164xxxx.. from my dial-pad i don't get the contact details, Also you can see that in the method above i have to run a loop to find the matching contact even if there is a match, so is there a better method out there to do the same

Thanks

4

1 回答 1

0

我创建了一个更好的解决方案,可以在这里看到http://codesnight.blogspot.in/2013/11/ios-acces-contacts-from-number.html

于 2013-11-28T10:38:16.553 回答