1

我很难取到iPhone contacts.

  • 我试图通过以下代码获取联系人。
  • 它在模拟器中运行良好,并且在联系人列表中的联系人较少时也运行良好。
  • 在我的手机中,我有 1000 个联系人。所以它在这个设备上崩溃了。如果您知道原因,请指导我。

这是我的代码

     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];

  }

提前致谢

4

2 回答 2

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);
    });
});
}

使用此代码肯定会对您有所帮助.. 作为参考,您可以使用此链接https://github.com/qnibus/TKContactsMultiPicker ... :)

于 2014-05-29T05:43:59.653 回答
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];
于 2014-05-29T06:34:58.413 回答