我正在构建一个iOS应用程序,我试图获取所有手机联系人并显示它们。
现在的问题是,如果有两个电话号码用一个名字保存的情况,那么在我的列表中只显示第一个号码。
我需要检索针对某个特定联系人保存的两个号码。
答案 0 :(得分:1)
通过方法shouldContinueAfterSelectingPerson
,您可以获得所选person
的参考,您可以获得不同的电话号码,
multi = ABRecordCopyValue(person, kABPersonPhoneProperty);
for(i=0; i< ABMultiValueGetCount(multi);i++)
{
NSMutableString *numb = (__bridge NSMutableString*)ABMultiValueCopyValueAtIndex(multi, i);
//next 5 line will make numbers are clear digit, without symbols.
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@" " withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"(" withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@")" withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"-" withString:@""]];
numb = [NSMutableString stringWithFormat:@"%@",[numb stringByReplacingOccurrencesOfString:@"." withString:@""]];
NSString *valAtIndex = (__bridge NSString *)ABMultiValueCopyLabelAtIndex(multi,i);
if([valAtIndex isEqualToString:@"_$!<Mobile>!$_"] || [valAtIndex isEqualToString:@"_$!<Other>!$_"])
mobileNumber = numb;
if ([valAtIndex isEqualToString:@"_$!<Home>!$_"])
homeNumber = numb;
if ([valAtIndex isEqualToString:@"_$!<Work>!$_"])
WorkNuimber = numb;
}
答案 1 :(得分:0)
可以在此处找到有关iOS地址簿的精彩教程:
http://www.raywenderlich.com/63885/address-book-tutorial-in-ios
答案 2 :(得分:0)
// Import two framework
#import <AddressBookUI/AddressBookUI.h>
#import <AddressBook/AddressBook.h>
// create a property
@property (nonatomic, assign) ABAddressBookRef addressBookRef;
// in view didLoad
ABAddressBookRequestAccessWithCompletion(self.addressBookRef, ^(bool granted, CFErrorRef error) {
if (granted) {
dispatch_async(dispatch_get_main_queue(), ^{
[self getContactsFromAddressBook];
});
} else {
// TODO: Show alert
}
});
-(void)getContactsFromAddressBook
{
CFErrorRef error = NULL;
ABAddressBookRef addressBook = ABAddressBookCreateWithOptions(NULL, &error);
if (addressBook) {
NSArray *allContacts = (__bridge_transfer NSArray*)ABAddressBookCopyArrayOfAllPeople(addressBook);
NSUInteger i = 0;
for (i = 0; i<[allContacts count]; i++) {
// myClass *shrObj = [[myClass alloc] init];
ABRecordRef contactPerson = (__bridge ABRecordRef)allContacts[i];
// Get first and last names
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonFirstNameProperty);
NSString *lastName= (__bridge_transfer NSString*)ABRecordCopyValue(contactPerson, kABPersonLastNameProperty);
if (!lastName) {
lastName=@"";
}
NSString *fullName = [NSString stringWithFormat:@"%@ %@",firstName,lastName];
NSMutableDictionary *dict = [[NSMutableDictionary alloc]init];
[dict setValue:fullName forKey:@"name"];
// Get mobile number
ABMultiValueRef phonesRef = ABRecordCopyValue(contactPerson, kABPersonPhoneProperty);
if ([self getMobilePhoneProperty:phonesRef]!=nil) {
[dict setValue:[self getMobilePhoneProperty:phonesRef] forKey:@"mobile"];
[arrContact addObject:dict];
}
if(phonesRef) {
CFRelease(phonesRef);
}
}
[tblContact reloadData];
}
else
{
NSLog(@"Error");
}
}
- (NSString *)getMobilePhoneProperty:(ABMultiValueRef)phonesRef {
for (int i=0; i < ABMultiValueGetCount(phonesRef); i++) {
CFStringRef currentPhoneLabel = ABMultiValueCopyLabelAtIndex(phonesRef, 0);
CFStringRef currentPhoneValue = ABMultiValueCopyValueAtIndex(phonesRef, 0);
if(currentPhoneLabel) {
if (CFStringCompare(currentPhoneLabel, kABPersonPhoneMobileLabel, 0) == kCFCompareEqualTo) {
return (__bridge NSString *)currentPhoneValue;
}
if (CFStringCompare(currentPhoneLabel, kABHomeLabel, 0) == kCFCompareEqualTo) {
return (__bridge NSString *)currentPhoneValue;
}
}
if(currentPhoneLabel) {
CFRelease(currentPhoneLabel);
}
if(currentPhoneValue) {
CFRelease(currentPhoneValue);
}
}
return nil;
}