【发布时间】:2012-01-23 11:22:29
【问题描述】:
我正在开发一个应用程序,但现在我停留在这一点(如何获取 iphone 通讯簿中所有联系人的街道、邮编、州、国家/地区)。任何示例代码都会很有帮助。
【问题讨论】:
标签: iphone objective-c
我正在开发一个应用程序,但现在我停留在这一点(如何获取 iphone 通讯簿中所有联系人的街道、邮编、州、国家/地区)。任何示例代码都会很有帮助。
【问题讨论】:
标签: iphone objective-c
您可以获得所有联系人的地址 -
ABAddressBookRef addressBook = ABAddressBookCreate();
NSArray *contactArr = (NSArray *)ABAddressBookCopyArrayOfAllPeople(addressBook);
for (int i = 0; i < [contactArr count]; i++)
{
ABRecordRef person = (ABRecordRef)[contactArr objectAtIndex:i];
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
for(CFIndex j = 0; j < ABMultiValueGetCount(address); j++)
{
CFDictionaryRef addressDict = ABMultiValueCopyValueAtIndex(address, j);
CFStringRef streetValue = CFDictionaryGetValue(addressDict, kABPersonAddressStreetKey);
CFStringRef cityValue = CFDictionaryGetValue(addressDict, kABPersonAddressCityKey);
CFStringRef stateValue = CFDictionaryGetValue(addressDict, kABPersonAddressStateKey);
CFStringRef zipValue = CFDictionaryGetValue(addressDict, kABPersonAddressZIPKey);
CFStringRef countryValue = CFDictionaryGetValue(addressDict, kABPersonAddressCountryKey);
}
}
【讨论】:
我知道这个问题很老,但这是一个基于 saadnib 答案的编辑版本。它遵循ARC并尊重授权状态。 该方法返回一个 NSMutableDictionary,其中包含人的姓名以及一个或多个地址(通过 ID 连接)。
- (NSMutableDictionary *)MyGetAddressesAndNamesOfContacts
{
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusDenied)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No permission" message:@"This App has no permission to access your contacts." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
return nil;
}
if (ABAddressBookGetAuthorizationStatus() == kABAuthorizationStatusNotDetermined)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Why this App needs your contacts" message:@"In the following your device will ask you whether this App is allowed to access your contacts. This is recommented because..." delegate:self cancelButtonTitle:@"I understand" otherButtonTitles: nil];
[alert show];
}
ABAddressBookRef addressBook = ABAddressBookCreate(); // deprecated since iOS 6
NSArray *contactArr = (NSArray *)CFBridgingRelease(ABAddressBookCopyArrayOfAllPeople(addressBook));
NSMutableDictionary *dPersons = [[NSMutableDictionary alloc] init];
for (int i = 0; i < [contactArr count]; i++)
{
ABRecordRef person = (ABRecordRef)CFBridgingRetain([contactArr objectAtIndex:i]);
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSString *sPersonName = [NSString stringWithFormat:@"%@ %@", firstName, lastName];
ABMultiValueRef address = ABRecordCopyValue(person, kABPersonAddressProperty);
NSString *sAddress;
for(CFIndex j = 0; j < ABMultiValueGetCount(address); j++)
{
CFDictionaryRef addressDict = ABMultiValueCopyValueAtIndex(address, j);
CFStringRef streetValue = CFDictionaryGetValue(addressDict, kABPersonAddressStreetKey);
CFStringRef cityValue = CFDictionaryGetValue(addressDict, kABPersonAddressCityKey);
CFStringRef stateValue = CFDictionaryGetValue(addressDict, kABPersonAddressStateKey);
CFStringRef zipValue = CFDictionaryGetValue(addressDict, kABPersonAddressZIPKey);
CFStringRef countryValue = CFDictionaryGetValue(addressDict, kABPersonAddressCountryKey);
sAddress = [NSString stringWithFormat:@"%@ %@, %@", streetValue, cityValue, countryValue];
[dPersons setObject:sAddress forKey: [NSString stringWithFormat:@"%@%d %@%ld", @"AddressFromNameID", i, @"Number", j]];
}
[dPersons setObject:sPersonName forKey: [NSString stringWithFormat:@"%@%d", @"NameWithID", i]];
}
return dPersons;
}
【讨论】:
NSArray *addresses = (NSArray*)[contact.postalAddresses valueForKey:@"value"];
if (!(addresses == nil) && addresses.count > 0)
{
for (CNLabeledValue<CNPostalAddress*>* labeledValue in contact.postalAddresses)
{
NSString *city = labeledValue.value.city;
NSLog(@"City = %@",city);
NSString *street = labeledValue.value.street;
NSLog(@"Street = %@",street);
NSString *state = labeledValue.value.state;
NSLog(@"State = %@",state);
NSString *postalCode = labeledValue.value.postalCode;
NSLog(@"PostalCode = %@",postalCode);
NSString *ISOCountryCode = labeledValue.value.ISOCountryCode;
NSLog(@"ISOCountryCode = %@",ISOCountryCode);
}
}
else
{
NSLog(@"No addresses for name = %@",strname);
}
【讨论】: