I faced one problem from the address book,i saved two phone numbers from my contacts one as home other one as office,i am getting only one number in ,application contact list.if i want to get the two numbers what i want to do.
Asked
Active
Viewed 74 times
-1
-
try this : http://stackoverflow.com/questions/17975297/retrieve-all-contacts-phone-numbers-in-ios – Huy Nghia Jan 29 '15 at 06:11
-
Post some code please. – Lyndsey Scott Jan 29 '15 at 06:25
-
@LyndseyScott :thank you Lyndsey for try to solve my problem but,i solved this problem,just little bit mistake is there in the loop. – Mahesh Reddy Jan 29 '15 at 07:41
1 Answers
0
With this code you will fetch all contact names from address book.I hope it will help you.
-(void)addressbookfetch
{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
CFArrayRef people=ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef peopleMutable = CFArrayCreateMutableCopy(
kCFAllocatorDefault,
CFArrayGetCount(people),
people
);
CFArraySortValues(
peopleMutable,
CFRangeMake(0, CFArrayGetCount(peopleMutable)),
(CFComparatorFunction) ABPersonComparePeopleByName,
(void*) ABPersonGetSortOrdering()
);
if(addressBook != nil)
{
NSLog(@"Succesful.");
NSArray *allContacts = (__bridge_transfer NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for(i=0;i<[allContacts count];i++)
{
Person *person = [[Person alloc]init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
ABMultiValueRef phoneNumber = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
NSUInteger k=0;
for(k=0;k<ABMultiValueGetCount(phoneNumber);k++)
{
NSString *firstName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString *)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName ?: @"",lastName ?: @""];
fullName = [fullName stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
[addressData addObject:fullName];
NameStr = [[NSMutableString alloc] initWithString:fullName];
NameStrnew=[NSString stringWithString:fullName];
NSLog(@"Name string %@",NameStrnew);
[phoneBookNames addObject:NameStrnew];
NSString *phonenumber= (__bridge_transfer NSString *)ABMultiValueCopyValueAtIndex(phoneNumber, k);
if(k==0)
{
person.homePhone = phonenumber;
PhoneStr = [[NSMutableString alloc] initWithString:phonenumber];
PhoneStrnew =[NSString stringWithString:phonenumber];
NSLog(@"PhoneStrnew%@",PhoneStrnew);
// [addressData addObject:phonenumber];
[phoneBookNumbers addObject:PhoneStrnew];
}

Gursharn Kaur
- 148
- 8