如何从ABPeoplePickerNavigationController获取街道地址

时间:2011-11-30 16:04:42

标签: iphone objective-c ios addressbook

我需要联系人的街道地址。我知道如何获取单值属性,但街道地址是多值属性。 Apple的文档显示了如何设置它,但没有检索它。有什么帮助吗?

PS:这不起作用:

ABRecordCopyValue(person, kABPersonAddressStreetKey);

5 个答案:

答案 0 :(得分:9)

我刚想通了:

ABMultiValueRef st = ABRecordCopyValue(person, kABPersonAddressProperty);
if (ABMultiValueGetCount(st) > 0) {
    CFDictionaryRef dict = ABMultiValueCopyValueAtIndex(st, 0);
    self.street.text = CFDictionaryGetValue(dict, kABPersonAddressStreetKey);
}

答案 1 :(得分:3)

Swift版本:

    if let addresses : ABMultiValueRef = ABRecordCopyValue(person, kABPersonAddressProperty)?.takeRetainedValue() as ABMultiValueRef? where ABMultiValueGetCount(addresses) > 0 {
        for index in 0..<ABMultiValueGetCount(addresses){
            if let address = ABMultiValueCopyValueAtIndex(addresses, index)?.takeRetainedValue() as? [String:String],
                label = ABMultiValueCopyLabelAtIndex(addresses, index)?.takeRetainedValue()  as? String{
                    print("\(label): \(address) \n")
            }
        }
    }

您可以通过提供相应的密钥来访问单个地址字段:

let street  = address[kABPersonAddressStreetKey as String]
let city    = address[kABPersonAddressCityKey as String]
let state   = address[kABPersonAddressStateKey as String]
let zip     = address[kABPersonAddressZIPKey as String]
let country = address[kABPersonAddressCountryKey as String]
let code    = address[kABPersonAddressCountryCodeKey as String]

答案 2 :(得分:2)

Swift 3.0

/**
 * @param {Object} browserHistory 
 * @param {string} route
 */
const push = browserHistory => route => browserHistory.push(route)

答案 3 :(得分:1)

如果用户有多个已定址的地址 - work,home等,则需要使用identifier属性来区分它们。从电子邮件地址上的类似帖子中挑选出来的是:

#pragma mark - ABPeoplePickerNavigationControllerDelegate

- (IBAction)chooseContact:(id)sender
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;
    [self presentViewController:picker animated:YES completion:nil];
//    [self dismissViewControllerAnimated:YES completion:nil];
}


- (void) peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}


- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
    return YES;
}


- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker
      shouldContinueAfterSelectingPerson:(ABRecordRef)person
                                property:(ABPropertyID)property
                              identifier:(ABMultiValueIdentifier)identifier
{
    if (property == kABPersonAddressProperty)
    {
        ABMultiValueRef addresses = ABRecordCopyValue(person, property);
        CFIndex addressIndex = ABMultiValueGetIndexForIdentifier(addresses, identifier);
        CFDictionaryRef address = ABMultiValueCopyValueAtIndex(addresses, addressIndex);

        // create address string to lookup
        NSString *street = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStreetKey);
        NSString *city = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCityKey);
        NSString *state = (NSString*) CFDictionaryGetValue(address, kABPersonAddressStateKey);
        NSString *postal = (NSString*) CFDictionaryGetValue(address, kABPersonAddressZIPKey);
        NSString *country = (NSString*) CFDictionaryGetValue(address, kABPersonAddressCountryKey);

        CFRelease(address);
        CFRelease(addresses);
        [self dismissViewControllerAnimated:YES completion:nil];
        return NO;
    }

    return YES;
}

答案 4 :(得分:0)

在iOS 9中不推荐使用地址簿UI框架。请改用ContactsUI框架中定义的API。要了解详情,请参阅ContactsUI