1

I am having the trouble to fetch iPhone contacts.

  • I have tried to fetch the contact by the following code.
  • It is working fine in simulator and also worked fine when the contacts are less in the contact list.
  • In my phone I am having 1000 Contacts. So it crashes on this device. Please guide me if you know the reason.

Here is my code.

     ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, error);
        ABRecordRef source = ABAddressBookCopyDefaultSource(addressBook);
        CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeopleInSourceWithSortOrdering(addressBook, source, kABPersonSortByFirstName);
        CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);
        NSMutableArray* items = [NSMutableArray arrayWithCapacity:nPeople];


        for (int i = 0; i < nPeople; i++)
        {
            NSMutableDictionary *dicContacts = [[NSMutableDictionary alloc]init];

            ABRecordRef person = CFArrayGetValueAtIndex(allPeople, i);
      NSMutableArray *phoneNumbers = [[NSMutableArray alloc] init];

            ABMultiValueRef multiPhones = ABRecordCopyValue(person, kABPersonPhoneProperty);
            for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++)
            {
                CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
                NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
                [phoneNumbers addObject:phoneNumber];
                //NSLog(@"All numbers %@", phoneNumbers);

            }

            if ([phoneNumbers count] > 0) {
                [dicContacts setValue:[phoneNumbers objectAtIndex:0] forKeyPath:@"Contact"];
            }

            [items addObject:dicContacts];

  }

Thanks In Advance

Rajesh Loganathan
  • 11,129
  • 4
  • 78
  • 90
Rythm
  • 191
  • 1
  • 16

2 Answers2

0
-(void)tkPeoplePickerController:(TKPeoplePickerController*)picker didFinishPickingDataWithInfo:(NSArray*)contacts
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    CFErrorRef *error = nil;
    ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL,error);
    [contacts enumerateObjectsUsingBlock:^(id obj,NSUInteger idx, BOOL *stop)
     {
         NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

         TKContact *contact = (TKContact*)obj;
         NSNumber *personID = [NSNumber numberWithInt:contact.recordID];
         ABRecordID abRecordID = (ABRecordID)[personID intValue];
         ABRecordRef abPerson = ABAddressBookGetPersonWithRecordID(addressBook, abRecordID);

         // Check person image
         UIImage *personImage = nil;
         if (abPerson != nil && ABPersonHasImageData(abPerson))
         {
             if ( &ABPersonCopyImageDataWithFormat != nil )
             {
                 // iOS >= 4.1
                 CFDataRef contactThumbnailData = ABPersonCopyImageDataWithFormat(abPerson, kABPersonImageFormatThumbnail);
                 personImage = [UIImage imageWithData:(__bridge NSData*)contactThumbnailData];
                 CFRelease(contactThumbnailData);
                 ABMultiValueRef emailValues = ABRecordCopyValue(abPerson, kABPersonEmailProperty);
                 // This is how you extract the first item from the multivalues:
                 CFStringRef cfEmailAddress = ABMultiValueCopyValueAtIndex(emailValues, 0);
                 // This is how you convert it to an NSString.

                 [RSVP_ImageArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                             contact.name,@"name",
                                             contact.email,@"email",
                                             [NSString stringWithFormat:@"%d",abRecordID],@"recodeID",
                                             savedImagePath,@"Image",
                                             nil]];
                 CFDataRef contactImageData = ABPersonCopyImageDataWithFormat(abPerson, kABPersonImageFormatOriginalSize);
                 CFRelease(contactImageData);
             }
             else
             {
                 // iOS < 4.1
                 CFDataRef contactImageData = ABPersonCopyImageData(abPerson);
                 personImage = [[UIImage imageWithData:(__bridge NSData*)contactImageData] thumbnailImage:CGSizeMake(thumbnailSize, thumbnailSize)];
                 ABMultiValueRef emailValues = ABRecordCopyValue(abPerson, kABPersonEmailProperty);
                 // This is how you extract the first item from the multivalues:
                 CFStringRef cfEmailAddress = ABMultiValueCopyValueAtIndex(emailValues, 0);
                 // This is how you convert it to an NSString.

                [RSVP_ImageArray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                             contact.name,@"name",
                                             contact.email,@"email",
                                             [NSString stringWithFormat:@"%d",abRecordID],@"recodeID",
                                             savedImagePath,@"Image",
                                             nil]];
                 CFRelease(contactImageData);
             }
         }
         else
         {
             ABMultiValueRef emailValues = ABRecordCopyValue(abPerson, kABPersonEmailProperty);
             // This is how you extract the first item from the multivalues:
             CFStringRef cfEmailAddress = ABMultiValueCopyValueAtIndex(emailValues, 0);
             // This is how you convert it to an NSString.

             [mutablearray addObject:[NSDictionary dictionaryWithObjectsAndKeys:
                                         contact.name,@"name",
                                         contact.email,@"email",
                                         [NSString stringWithFormat:@"%d",abRecordID],@"recodeID",
                                         savedImagePath,@"Image",
                                         nil]];
         }
     }
     dispatch_async(dispatch_get_main_queue(), ^{
    });

     [pool drain];
     }];

    dispatch_async(dispatch_get_main_queue(), ^{
        CFRelease(addressBook);
    });
});
}

Use this code with it will surely help you.. for reference u can use this link https://github.com/qnibus/TKContactsMultiPicker ... :)

Jasveer
  • 209
  • 2
  • 8
0
for(CFIndex i=0;i<ABMultiValueGetCount(multiPhones);i++)
{
    CFStringRef phoneNumberRef = ABMultiValueCopyValueAtIndex(multiPhones, i);
    NSString *phoneNumber = (__bridge NSString *) phoneNumberRef;
    [phoneNumbers addObject:[NSString stringWithFormat:@"%@",phoneNumber]];
    //NSLog(@"All numbers %@", phoneNumbers);
}
[phoneNumbers retain];
Bhavesh Nayi
  • 3,626
  • 1
  • 27
  • 42