0

I am building an iOS application where I am trying to get all the phone contacts and display them.

Now the problem is that if there is a case where are two phone numbers are saved with one name then in my list only the first number is displayed.

I need to retrieve both numbers saved against one particular contact.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
sof question
  • 93
  • 2
  • 13

4 Answers4

1

Please check apple standard addressbook tutorial. It will also help you to understand it's basics.

https://developer.apple.com/library/ios/documentation/ContactData/Conceptual/AddressBookProgrammingGuideforiPhone/Chapters/QuickStart.html

You can get all types of contacts by [PJRAddressBook getPersonContacts]; in my sample.where you can get all phone numbers. like mobilePhone, mainPhone,iPhonePhone,homeFaxPhone,workFaxPhone,otherFaxPhone,pagerPhone

PJR
  • 13,052
  • 13
  • 64
  • 104
  • Please don't just post a link as that can go out of date. Instead add whatever you need to say into your answer. – trojanfoe May 01 '15 at 07:19
  • @PJR Thanks but this blog is adding the contact I wanna to retrieve the contact and display. – sof question May 01 '15 at 07:34
  • @sofquestion i am sharing my one component which will help you:https://github.com/paritsohraval100/PJRAddressBook – PJR May 01 '15 at 09:30
  • @PJR Thanks for sharing the code but I am facing the same problem as - I have two number store in my contact list of name Hitesh but it is showing first number only. can you please help me in this problem. – sof question May 01 '15 at 10:07
  • Can you please help me where I should use this above line in your sample code in which class and where. As I am new to IOS i have less understanding of this – sof question May 01 '15 at 10:16
  • @just follow methods which shown in demo. It is very easy .just copy paste. – PJR May 01 '15 at 10:19
  • I have added as follow - NSLog(@"mob:%@",contact.mobilePhone); NSLog(@"second mob:%@",contact.mainPhone); NSLog(@"second mob:%@",contact.iPhonePhone); NSLog(@"second mob:%@",contact.workFaxPhone); NSLog(@"second mob:%@",contact.otherFaxPhone); NSLog(@"second mob:%@",contact.pagerPhone); NSLog(@"second mob:%@",contact.homeFaxPhone); NSLog(@"iphone:%@",contact.iPhonePhone); yet it is not showing my second number – sof question May 01 '15 at 10:26
  • I am again explaining my problem please help - I have two number save in my addressbook - 1234567891 as tag ( work ), the other number - 0987654321 as tag ( mobile ) with the name abcd in log is show first name - abcd mobile number 0987654321, reSt is null. – sof question May 01 '15 at 10:35
  • Please help PJR I am working on this issue from morning it will be great if you help. – sof question May 01 '15 at 10:36
1

From the method shouldContinueAfterSelectingPerson you can get reference of selected person, you can get different phone numbers like,

multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(i=0; i< ABMultiValueGetCount(multi);i++)
{
    NSMutableString *numb = (__bridge NSMutableString*)ABMultiValueCopyValueAtIndex(multi, i);

    //next 5 line will make numbers are clear digit, without symbols. 
    numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@" " withString:@""]];
    numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"(" withString:@""]];
    numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@")" withString:@""]];
    numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"-" withString:@""]];
    numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"." withString:@""]];

    NSString *valAtIndex = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(multi,i);


    if([valAtIndex isEqualToString:@"_$!<Mobile>!$_"] || [valAtIndex isEqualToString:@"_$!<Other>!$_"])
        mobileNumber = numb;

    if ([valAtIndex isEqualToString:@"_$!<Home>!$_"])
        homeNumber = numb;

    if ([valAtIndex isEqualToString:@"_$!<Work>!$_"])
        WorkNuimber = numb;
}
Viral Savaj
  • 3,379
  • 1
  • 26
  • 39
0

Good tutorials on Address Book for iOS can be found here:

http://www.raywenderlich.com/63885/address-book-tutorial-in-ios

Daniel
  • 8,794
  • 4
  • 48
  • 71
0
// Import two framework

#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>

// create a property

@property (nonatomic, assign) ABAddressBookRef addressBookRef;


// in view didLoad

ABAddressBookRequestAccessWithCompletion(self.addressBookRef, ^(bool granted, CFErrorRef error) {
    if (granted) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [self getContactsFromAddressBook];
        });
    } else {
        // TODO: Show alert
    }
});


-(void)getContactsFromAddressBook
{
    CFErrorRef error = NULL;

    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
    if (addressBook) {
        NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);

        NSUInteger i = 0;
        for (i = 0; i<[allContacts count]; i++)  {
            //  myClass *shrObj = [[myClass alloc] init];
            ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];

            // Get first and last names
            NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);

            NSString *lastName= (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);

            if (!lastName) {
                lastName=@"";
            }

            NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];

            NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];

            [dict setValue:fullName forKey:@"name"];

            // Get mobile number
            ABMultiValueRef phonesRef = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);

            if ([self getMobilePhoneProperty:phonesRef]!=nil) {

                [dict setValue:[self getMobilePhoneProperty:phonesRef] forKey:@"mobile"];
                [arrContact addObject:dict];

            }

            if(phonesRef) {
                CFRelease(phonesRef);
            }

        }

        [tblContact reloadData];
    }
    else
    {
        NSLog(@"Error");

    }
}

- (NSString *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef {
    for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {
        CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, 0);
        CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, 0);

        if(currentPhoneLabel) {
            if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
                return (__bridge NSString *)currentPhoneValue;
            }

            if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
                return (__bridge NSString *)currentPhoneValue;
            }
        }
        if(currentPhoneLabel) {
            CFRelease(currentPhoneLabel);
        }
        if(currentPhoneValue) {
            CFRelease(currentPhoneValue);
        }
    }

    return nil;
}
Bista
  • 7,869
  • 3
  • 27
  • 55
Ankit Kargathra
  • 309
  • 3
  • 16