How to get street address from ABPeoplePickerNavigationController

谁都会走 提交于 2019-12-18 17:30:45

问题


I need a contact's street address. I know how to get the single value properties, but the street address is a multivalue property. Apple's documentation shows how to set it, but not retrieve it. Any help?

PS: this does not work:

ABRecordCopyValue(person, kABPersonAddressStreetKey);

回答1:


I just figured it out:

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



回答2:


Swift version :

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

You can access individual field of address by providing corresponding key :

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]



回答3:


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"
}



回答4:


If the user has multiple addressed defined - work, home, etc, you will need to use the identifier attribute to distinguish between them. What I have arrive at, culled from similar posts on email addresses, is:

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

.




回答5:


I suppose it should work like this (derived from the documentation, not tested):

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



回答6:


The Address Book UI framework is deprecated in iOS 9. Use the APIs defined in the ContactsUI framework instead. To learn more, see ContactsUI.



来源:https://stackoverflow.com/questions/8329006/how-to-get-street-address-from-abpeoplepickernavigationcontroller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!