如何从ABPeoplePickerNavigationController获取街道地址



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

PS: this does not work:

ABRecordCopyValue(person, kABPersonAddressStreetKey);

我刚刚想通了:

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

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]

Swift 3.0

//Extract billing address from ABRecord format and assign accordingly
let addressProperty: ABMultiValue = ABRecordCopyValue(billingAddress, kABPersonAddressProperty).takeUnretainedValue() as ABMultiValue
if let dict: NSDictionary = ABMultiValueCopyValueAtIndex(addressProperty, 0).takeUnretainedValue() as? NSDictionary {
     print(dict[String(kABPersonAddressStreetKey)] as? String)
     print(dict[String(kABPersonAddressCityKey)] as? String)
     print(dict[String(kABPersonAddressStateKey)] as? String)
     print(dict[String(kABPersonAddressZIPKey)] as? String)
     print(dict[String(kABPersonAddressCountryKey)] as? String) //"United States"
}

如果用户定义了多个地址——工作地址、家庭地址等,则需要使用标识符属性来区分它们。我从电子邮件地址的类似帖子中挑选出来的是:

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

.

我想它应该是这样工作的(来自文档,未测试):

ABMultiValueRef addressMultiValue = ABRecordCopyValue(person, kABPersonAddressProperty);
CFArrayRef allAddresses = ABMultiValueCopyArrayOfAllValues(addressMultiValue);
CFDictionaryRef firstAddress = CFArrayGetValueAtIndex(allAddresses, 0);
CFStringRef street = CFDictionaryGetValue(firstAddress, kABPersonAddressStreetKey);
NSLog(@"%@", (__bridge NSString *)street);
CFRelease(allAddresses);
CFRelease(addressMultiValue);

地址本UI框架在iOS 9中已弃用。请使用ContactsUI框架中定义的api。要了解更多信息,请参见ContactsUI。

最新更新