我正在检索联系人的多个地址,但只想要家庭住址

时间:2013-06-25 15:03:43

标签: ios abaddressbook abmultivalue

有谁知道如何从iOS中的多地址检索家庭住址?我得到了用户和其他所有人的许可,但问题是,我只想要家庭住址。

ABAddressBookRef addressBook = ABAddressBookCreate();
__block BOOL accessGranted = NO;
if (ABAddressBookRequestAccessWithCompletion != NULL) { // we're on iOS 6
    dispatch_semaphore_t sema = dispatch_semaphore_create(0);
    ABAddressBookRequestAccessWithCompletion(addressBook, ^(bool granted, CFErrorRef error) {
        accessGranted = granted;
        dispatch_semaphore_signal(sema);
    });
    dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);
    dispatch_release(sema);
}
else { // we're on iOS 5 or older

    accessGranted = YES;
}

if (accessGranted) {

    CFArrayRef allPeople = ABAddressBookCopyArrayOfAllPeople(addressBook);

    CFIndex nPeople = ABAddressBookGetPersonCount(addressBook);

    NSMutableArray *array = [[NSMutableArray alloc] init];

    for( int i = 0 ; i < nPeople ; i++ )

    {
        NSMutableDictionary *dic = [[NSMutableDictionary alloc] init];

        ABRecordRef ref = CFArrayGetValueAtIndex(allPeople, i );

        // First Name
        NSString *fName = (__bridge NSString*)ABRecordCopyValue(ref, kABPersonFirstNameProperty);

        // Last Name
        NSString *lName = (__bridge NSString*)ABRecordCopyValue(ref, kABPersonLastNameProperty);

        // Phone
        ABMultiValueRef phoneMultiValue = ABRecordCopyValue(ref, kABPersonPhoneProperty);
        CFArrayRef allPhones = ABMultiValueCopyArrayOfAllValues(phoneMultiValue);
        NSMutableArray *phoneData = [NSMutableArray arrayWithArray:(__bridge NSArray*) allPhones];

        // Email
        ABMultiValueRef emailMultiValue = ABRecordCopyValue(ref, kABPersonEmailProperty);
        CFArrayRef allEmails = ABMultiValueCopyArrayOfAllValues(emailMultiValue);
        NSMutableArray *emailData = [NSMutableArray arrayWithArray:(__bridge NSArray*) allEmails];

        // Address
        ABMultiValueRef addressMultiValue = ABRecordCopyValue(ref, kABPersonAddressProperty);

        CFArrayRef allAddresses = ABMultiValueCopyArrayOfAllValues(addressMultiValue);

        NSMutableArray* addressData = [NSMutableArray arrayWithArray:(__bridge NSArray*) allAddresses];

        for ( int j = 0 ; j < [addressData count]; j++) {
            if ([[addressData objectAtIndex:j] count] > 0) {

                if ([fName length] > 0 || [lName length] > 0) {


                    if ([fName length] > 0) {
                        [dic setObject:fName forKey:@"FirstName"];
                    }

                    if ([lName length] > 0) {
                        [dic setObject:lName forKey:@"LastName"];
                    }

                    if ([phoneData count] > 0) {
                        [dic setObject:phoneData forKey:@"MultiplePhoneNumbers"];
                    }

                    if ([emailData count] > 0) {
                        [dic setObject:emailData forKey:@"MultipleEmails"];
                    }


                    [dic setObject:addressData forKey:@"MultipleAddresses"];



                }
            }
        }

        NSUInteger keyCount = [[dic allKeys] count];
        if (keyCount > 0) {

            ABRecordID recId = ABRecordGetRecordID(ref);


            [dic setObject:[NSString stringWithFormat:@"%d",recId] forKey:@"ABRecordRef"];
            [dic setObject:[NSString stringWithFormat:@"%d",i] forKey:@"ab_id"];
            [dic setObject:[NSNumber numberWithBool:FALSE] forKey:@"is_coordinate_fetch"];
            [array addObject:dic];                
        }

如果有人能花时间为我解决这个问题,我真的很感激。

2 个答案:

答案 0 :(得分:1)

您需要遍历kABPersonAddressProperty多值属性并提取与kABHomeLabel标识符匹配的属性。以下是如何在iOS 7中执行此操作(假设对地址簿的引用):

NSArray *people = CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(localAddressBook));

// Iterate through each person in the Address Book
for (NSUInteger i = 0; i < people.count; i++)
{
    ABRecordRef person = CFArrayGetValueAtIndex((__bridge CFArrayRef)people, i);

    // Access the person's addresses (a ABMultiValueRef)
    ABMultiValueRef addressesProperty = CFAutorelease(ABRecordCopyValue(person, kABPersonAddressProperty));

    if (addressesProperty)
    {
        // Iterate through the address multivalue
        for (CFIndex index = 0; index < ABMultiValueGetCount(addressesProperty); index++)
        {
            // Get the address label
            NSString *addressLabel = (NSString *)CFBridgingRelease(ABMultiValueCopyLabelAtIndex(addressesProperty, index));

            // Check for home address label
            if ([addressLabel isEqualToString:(NSString *)kABHomeLabel])
            {
                // Your code here
                NSLog(@"%@", addressLabel);
            }
        }
    }
}

答案 1 :(得分:0)

这个问题没有单一的解决方案。我在我的应用程序中执行此操作的内容如下:

1)如果该人只有一个地址,请使用该地址。

2)如果有多个地址,使用具有名称和第一个地址的单元格,以及一个表示“选择不同地址”的按钮。如果用户点击该按钮,则会在用户看到所有地址的另一个表格的工作表中设置动画,并可以选择他们想要的那个。