簡體   English   中英

在iOS中獲取iPhone的聯系人列表時,應用程序崩潰

[英]Application crashes while fetching the iPhone's contact list in iOS

我在獲取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];

  }

提前致謝

-(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 ... :)

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM