[英]How to get street address from ABPeoplePickerNavigationController
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. Apple的文档显示了如何设置它,但没有检索它。 Any help? 有帮助吗?
PS: this does not work: PS:这不起作用:
ABRecordCopyValue(person, kABPersonAddressStreetKey);
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);
}
Swift version : 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")
}
}
}
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]
Swift 3.0 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"
}
If the user has multiple addressed defined - work, home, etc, you will need to use the identifier attribute to distinguish between them. 如果用户有多个已定义的地址 - work,home等,则需要使用identifier属性来区分它们。 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;
}
. 。
The Address Book UI framework is deprecated in iOS 9. Use the APIs defined in the ContactsUI framework instead. iOS 9中不推荐使用Address Book UI框架。请改用ContactsUI框架中定义的API。 To learn more, see ContactsUI . 有关详细信息,请参阅ContactsUI 。
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.