Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

displaying contacts in a UItableview in phone contacts order

Tags:

iphone

i am able to import the contacts from phone book and displaying them in a table view .but what i want to do is to display the contacts in the same order as they are in the phone book order.....

can any one please help me how to do that and my code is as follows

self.navigationController.navigationBar.tintColor = [UIColor grayColor];
    self.title = @"iPhone Contacts";
    [super viewDidLoad];
    wantedname= [[NSMutableArray alloc] init];
    wantednumber= [[NSMutableArray alloc] init];
    ABAddressBookRef addressBook = ABAddressBookCreate();
    NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

    NSString *name;
    for (id person in thePeople)
    {
        name = (NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
        NSLog(@"!!!!!! name ---> %@",name);
        ABMultiValueRef multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
        int count1=ABMultiValueGetCount(multi);
        NSLog(@"%d",count1);
        if ([name length]>0 && count1!=0) 
        {
                    NSString *beforenumber = (NSString *)ABMultiValueCopyValueAtIndex(multi, 0);
            NSLog(@" contacts:%@",beforenumber );
            NSString* removed1=[beforenumber stringByReplacingOccurrencesOfString:@"-"withString:@""];
            NSString* removed2=[removed1 stringByReplacingOccurrencesOfString:@")"withString:@""];
            NSString* removed3=[removed2 stringByReplacingOccurrencesOfString:@" "withString:@""];
            NSString* removed4=[removed3 stringByReplacingOccurrencesOfString:@"("withString:@""];
            NSString* removed5=[removed4 stringByReplacingOccurrencesOfString:@"+"withString:@""];
            [wantedname addObject:name];
            [wantednumber addObject:removed5];
           // CFRelease(beforenumber);
            [beforenumber release];
            //CFRelease(name);

        }
        //CFRelease(name);
        [name release];
        CFRelease(multi);
    }

    CFRelease(addressBook);
    CFRelease(thePeople);

    contactstable.delegate = self;
    contactstable.dataSource = self;
like image 932
user564963 Avatar asked May 03 '11 05:05

user564963


2 Answers

Instead of:

NSArray *thePeople = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);

You might try:

ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
NSArray *thePeople = (NSArray*)ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByLastName);
like image 58
Joe Avatar answered Sep 18 '22 06:09

Joe


This is how I did it. Hope it helps. :)

ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);

CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);
CFMutableArrayRef allPeopleMutable = CFArrayCreateMutableCopy(kCFAllocatorDefault, CFArrayGetCount(allPeople), allPeople);
CFArraySortValues(allPeopleMutable, CFRangeMake(0, CFArrayGetCount(allPeopleMutable)), (CFComparatorFunction)ABPersonComparePeopleByName, kABPersonSortByFirstName);
like image 35
user2390605 Avatar answered Sep 19 '22 06:09

user2390605